[llvm] [AMDGPU] Add IR LiveReg type-based optimization (PR #66838)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 14:42:46 PDT 2024


================
@@ -81,6 +81,73 @@ class AMDGPULateCodeGenPrepare
   bool visitLoadInst(LoadInst &LI);
 };
 
+using ValueToValueMap = DenseMap<const Value *, Value *>;
+
+class LiveRegOptimizer {
+private:
+  Module *Mod = nullptr;
+  const DataLayout *DL = nullptr;
+  const GCNSubtarget *ST;
+  /// The scalar type to convert to
+  Type *ConvertToScalar;
+  /// The set of visited Instructions
+  SmallPtrSet<Instruction *, 4> Visited;
+  /// The set of Instructions to be deleted
+  SmallPtrSet<Instruction *, 4> DeadInstrs;
+  /// Map of Value -> Converted Value
+  ValueToValueMap ValMap;
+  /// Map of containing conversions from Optimal Type -> Original Type per BB.
+  DenseMap<BasicBlock *, ValueToValueMap> BBUseValMap;
+
+public:
+  /// Calculate the and \p return  the type to convert to given a problematic \p
+  /// OriginalType. In some instances, we may widen the type (e.g. v2i8 -> i32).
+  Type *calculateConvertType(Type *OriginalType);
+  /// Convert the virtual register defined by \p V to the compatible vector of
+  /// legal type
+  Value *convertToOptType(Instruction *V, BasicBlock::iterator &InstPt);
+  /// Convert the virtual register defined by \p V back to the original type \p
+  /// ConvertType, stripping away the MSBs in cases where there was an imperfect
+  /// fit (e.g. v2i32 -> v7i8)
+  Value *convertFromOptType(Type *ConvertType, Instruction *V,
+                            BasicBlock::iterator &InstPt,
+                            BasicBlock *InsertBlock);
+  /// Check for problematic PHI nodes or cross-bb values based on the value
+  /// defined by \p I, and coerce to legal types if necessary. For problematic
+  /// PHI node, we coerce all incoming values in a single invocation.
+  bool optimizeLiveType(Instruction *I);
+
+  /// Remove all instructions that have become dead (i.e. all the re-typed PHIs)
+  void removeDeadInstrs();
+
+  // Whether or not the type should be replaced to avoid inefficient
+  // legalization code
+  bool shouldReplace(Type *ITy) {
+    FixedVectorType *VTy = dyn_cast<FixedVectorType>(ITy);
+    if (!VTy)
+      return false;
+
+    auto TLI = ST->getTargetLowering();
+
+    Type *EltTy = VTy->getElementType();
+    // If the element size is not less than the convert to scalar size, then we
+    // can't do any bit packing
+    if (!EltTy->isIntegerTy() ||
+        EltTy->getScalarSizeInBits() > (ConvertToScalar->getScalarSizeInBits()))
----------------
arsenm wrote:

```suggestion
        EltTy->getScalarSizeInBits() > ConvertToScalar->getScalarSizeInBits())
```

https://github.com/llvm/llvm-project/pull/66838


More information about the llvm-commits mailing list