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

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 22:00:08 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:

Right now, you have  `@llvm.spv.clip()` maps to `OpDemoteToHelperInvocation`  or `OpKill` depending on some extension availability. What I'm suggesting is twofold. First make your intrinsic map to the same number of arguments as your clang alias. Second that will mean that means register 1 to `selectClip` is what you will be used by `selectAny` Then do your branch invocations only in the spirv backend. In summary the instruction selection will be everything you need for this intrinsic not just OpDemoteToHelperInvocation`  or `OpKill`.

https://hlsl.godbolt.org/z/ose5EGWEx
```
%21 = OpFOrdLessThan %v4bool %20 %11
 %22 = OpAny %bool %21
             OpSelectionMerge %23 None
             OpBranchConditional %22 %24 %23
%24 = OpLabel
            OpKill | OpDemoteToHelperInvocation
 %23 = OpLabel
```

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


More information about the cfe-commits mailing list