[clang] [llvm] [HLSL] Re-implement countbits with the correct return type (PR #113189)

Justin Bogner via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 22 11:22:08 PDT 2024


================
@@ -461,6 +461,67 @@ class OpLowerer {
     });
   }
 
+  [[nodiscard]] bool lowerCtpopToCBits(Function &F) {
+    IRBuilder<> &IRB = OpBuilder.getIRB();
+    Type *Int32Ty = IRB.getInt32Ty();
+
+    return replaceFunction(F, [&](CallInst *CI) -> Error {
+      IRB.SetInsertPoint(CI);
+      SmallVector<Value *> Args;
+      Args.append(CI->arg_begin(), CI->arg_end());
+
+      Type *RetTy = Int32Ty;
+      Type *FRT = F.getReturnType();
+      if (FRT->isVectorTy()) {
+        VectorType *VT = cast<VectorType>(FRT);
+        RetTy = VectorType::get(RetTy, VT);
+      }
----------------
bogner wrote:

Better to use dyn_cast here rather than checking the type twice
```suggestion
      if (const auto *VT = dyn_cast<VectorType>(F.getReturnType()))
        RetTy = VectorType::get(RetTy, VT);
```

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


More information about the cfe-commits mailing list