llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Kunal Dubey  (xakep8)

<details>
<summary>Changes</summary>

CIRGen may produce undef constants while recovering from NYI builtin lowering. 
When such a value feeds an implicit integral cast, CastOp::fold currently 
treats the source as an integer constant solely because it is a cir.constant, 
then calls getIntValue() and crashes.

Added a check for the constant to see if the payload is actually an IntAttr 
before folding the cast. If it is not, return the cast unfolded.

Added a regression test covering an unimplemented __builtin_stdc_* call whose 
undef result was implicitly being cast to integer by CastOp::fold.

Part of issue #<!-- -->214443 

---
Full diff: https://github.com/llvm/llvm-project/pull/214551.diff


2 Files Affected:

- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+12-4) 
- (added) clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-nyi.c (+10) 


``````````diff
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index cd94219655e02..2e812ba61876b 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -975,6 +975,10 @@ OpFoldResult cir::CastOp::fold(FoldAdaptor adaptor) {
     return cir::PoisonAttr::get(getContext(), getType());
   }
 
+  // Propogate Undef value
+  if (mlir::isa_and_present<cir::UndefAttr>(adaptor.getSrc()))
+    return cir::UndefAttr::get(getType());
+
   if (getSrc().getType() == getType()) {
     switch (getKind()) {
     case cir::CastKind::integral: {
@@ -1013,10 +1017,14 @@ OpFoldResult cir::CastOp::fold(FoldAdaptor adaptor) {
 
       auto srcIntTy = mlir::cast<cir::IntType>(srcTy);
       auto dstIntTy = mlir::cast<cir::IntType>(getType());
-      APInt newVal =
-          srcIntTy.isSigned()
-              ? srcConst.getIntValue().sextOrTrunc(dstIntTy.getWidth())
-              : srcConst.getIntValue().zextOrTrunc(dstIntTy.getWidth());
+      auto constIntAttr = srcConst.getValueAttr<cir::IntAttr>();
+      if (!constIntAttr)
+        return {};
+
+      APInt srcValue = constIntAttr.getValue();
+      APInt newVal = srcIntTy.isSigned()
+                         ? srcValue.sextOrTrunc(dstIntTy.getWidth())
+                         : srcValue.zextOrTrunc(dstIntTy.getWidth());
       return cir::IntAttr::get(dstIntTy, newVal);
     }
     default:
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-nyi.c 
b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-nyi.c
new file mode 100644
index 0000000000000..2d0894da0bdbf
--- /dev/null
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-nyi.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir 
-verify %s -o - | FileCheck %s
+
+void test_stdc_trailing_zeros_undef_cast(unsigned long long x) {
+  // expected-error@+1 {{ClangIR code gen Not Yet Implemented: unimplemented 
builtin call: __builtin_stdc_trailing_zeros}}
+  int cnt = __builtin_stdc_trailing_zeros(x);
+  (void)cnt;
+}
+
+// CHECK-LABEL: test_stdc_trailing_zeros_undef_cast
+// CHECK: cir.const #cir.undef : !s32i

``````````

</details>


https://github.com/llvm/llvm-project/pull/214551
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to