[clang] [llvm] Add normalize builtins and normalize HLSL function to DirectX and SPIR-V backend (PR #102683)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 11 18:34:26 PDT 2024


================
@@ -229,6 +230,75 @@ static bool expandLog10Intrinsic(CallInst *Orig) {
   return expandLogIntrinsic(Orig, numbers::ln2f / numbers::ln10f);
 }
 
+static bool expandNormalizeIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  Type *Ty = Orig->getType();
+  Type *EltTy = Ty->getScalarType();
+  IRBuilder<> Builder(Orig->getParent());
+  Builder.SetInsertPoint(Orig);
+
+  auto *XVec = dyn_cast<FixedVectorType>(Ty);
+  if (!XVec) {
+    if (auto *constantFP = dyn_cast<ConstantFP>(X)) {
+      const APFloat &fpVal = constantFP->getValueAPF();
+      if (fpVal.isZero())
+        report_fatal_error(Twine("Invalid input scalar: length is zero"),
+                           /* gen_crash_diag=*/false);
+    }
+    Value *Result = Builder.CreateFDiv(X, X);
+
+    Orig->replaceAllUsesWith(Result);
+    Orig->eraseFromParent();
+    return true;
+  }
+
+  Value *Elt = Builder.CreateExtractElement(X, (uint64_t)0);
+  unsigned XVecSize = XVec->getNumElements();
+  Value *DotProduct = nullptr;
----------------
farzonl wrote:

For lines 256 to  279 is  something that will have to be cleaned up into a helper function. I think we are going to have some code duplication here  @pow2clk  is moving [`getDotProductIntrinsic`](https://github.com/llvm/llvm-project/blob/efd13eb305dcc394cd038d6c332df64e96276baa/clang/lib/CodeGen/CGBuiltin.cpp#L18365C1-L18381C2) here as an `expandFdot()` ideally we would have one function for this.  If we move forward with your PR before Greg's someone is going to need to clean this up.

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


More information about the cfe-commits mailing list