[clang] [llvm] [HLSL] Adding HLSL `clip` function. (PR #114588)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 12:13:03 PST 2024


================
@@ -99,6 +99,42 @@ static void initializeAlloca(CodeGenFunction &CGF, AllocaInst *AI, Value *Size,
   I->addAnnotationMetadata("auto-init");
 }
 
+static Value *handleHlslClip(const CallExpr *E, CodeGenFunction *CGF) {
+  Value *Op0 = CGF->EmitScalarExpr(E->getArg(0));
+
+  Constant *FZeroConst = ConstantFP::getZero(CGF->FloatTy);
+  Value *CMP;
+
+  if (const auto *VecTy = E->getArg(0)->getType()->getAs<clang::VectorType>()) {
+    FZeroConst = ConstantVector::getSplat(
+        ElementCount::getFixed(VecTy->getNumElements()), FZeroConst);
+    auto *FCompInst = CGF->Builder.CreateFCmpOLT(Op0, FZeroConst);
+    CMP = CGF->Builder.CreateIntrinsic(
+        CGF->Builder.getInt1Ty(), CGF->CGM.getHLSLRuntime().getAnyIntrinsic(),
+        {FCompInst}, nullptr);
+  } else
+    CMP = CGF->Builder.CreateFCmpOLT(Op0, FZeroConst);
+
+  if (CGF->CGM.getTarget().getTriple().isDXIL())
+    return CGF->Builder.CreateIntrinsic(CGF->VoidTy, llvm::Intrinsic::dx_clip,
----------------
farzonl wrote:

This is a bit different than how we have done intrinsics that invoke other intrinsics. For example see `expandNormalizeIntrinsic` in `llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp`  For example `dx_normaize` exapnds to:
```
fdiv
dx_dot
dx_rsqrt
```
While in this case we associate the instruction expansion with the  clang builtin. This isn't wrong, its just different as past implementers have tried to keep target specific behaviors in their respective targets. 

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


More information about the cfe-commits mailing list