[clang] [llvm] [SPIRV] Add Target Builtins using Distance ext as an example (PR #121598)

Farzon Lotfi via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 6 07:49:47 PST 2025


================
@@ -20440,6 +20442,26 @@ Value *CodeGenFunction::EmitAMDGPUBuiltinExpr(unsigned BuiltinID,
   }
 }
 
+Value *CodeGenFunction::EmitSPIRVBuiltinExpr(unsigned BuiltinID,
+                                             const CallExpr *E) {
+  switch (BuiltinID) {
+  case SPIRV::BI__builtin_spirv_distance: {
+    Value *X = EmitScalarExpr(E->getArg(0));
+    Value *Y = EmitScalarExpr(E->getArg(1));
+    assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
+           E->getArg(1)->getType()->hasFloatingRepresentation() &&
+           "Distance operands must have a float representation");
+    assert(E->getArg(0)->getType()->isVectorType() &&
+           E->getArg(1)->getType()->isVectorType() &&
+           "Distance operands must be a vector");
+    return Builder.CreateIntrinsic(
+        /*ReturnType=*/X->getType()->getScalarType(), Intrinsic::spv_distance,
+        ArrayRef<Value *>{X, Y}, nullptr, "spv.distance");
+  }
+  }
+  return nullptr;
----------------
farzonl wrote:

No see `EmitAMDGPUBuiltinExpr` and `EmitTargetArchBuiltinExpr` if a target builtin is not found the expected behavior is to return nullptr. All we know is that the builtin is not a targetArch builtin. Then when `EmitTargetBuiltinExpr` is used in `if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E, ReturnValue))` we can safely avoid a null deref. We need this to be a nullptr  and not `llvm_unreachable()` at this point so that their is a fall through to eventually check for lang builtins like `if (Value *V = EmitHLSLBuiltinExpr(BuiltinID, E, ReturnValue))`

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


More information about the llvm-commits mailing list