[clang] [llvm] Add length HLSL function to DirectX Backend (PR #101256)

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 1 11:11:22 PDT 2024


================
@@ -18460,6 +18460,20 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
         /*ReturnType=*/X->getType(), CGM.getHLSLRuntime().getLerpIntrinsic(),
         ArrayRef<Value *>{X, Y, S}, nullptr, "hlsl.lerp");
   }
+  case Builtin::BI__builtin_hlsl_elementwise_length: {
+    Value *X = EmitScalarExpr(E->getArg(0));
+
+    if (!E->getArg(0)->getType()->hasFloatingRepresentation())
+      llvm_unreachable("length operand must have a float representation");
----------------
bogner wrote:

`if (X) llvm_unreachable("...")` is a bit of an antipattern. Better to use assert in this kind of situation like so:
```suggestion
    assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
           "length operand must have a float representation");
```

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


More information about the llvm-commits mailing list