Author: Andrzej WarzyƄski
Date: 2026-06-02T15:52:17+01:00
New Revision: ade0e1a49c2118a98b0b34b3136661747bc5aaa0

URL: 
https://github.com/llvm/llvm-project/commit/ade0e1a49c2118a98b0b34b3136661747bc5aaa0
DIFF: 
https://github.com/llvm/llvm-project/commit/ade0e1a49c2118a98b0b34b3136661747bc5aaa0.diff

LOG: [cir] Refine cir::CastOp semantics for int <-> float casts (#200005)

Int-to-float and float-to-int casts in cir::CastOp are lowered directly
to their LLVM equivalents. Update the verifier to reflect this semantics
and ensure that, for vector casts, the source and destination vectors have
the same length.

This lets the CIR verifier reject invalid casts earlier, instead of relying
on errors reported later at the LLVM IR level.

Added: 
    

Modified: 
    clang/lib/CIR/Dialect/IR/CIRDialect.cpp
    clang/test/CIR/IR/invalid-cast.cir
    clang/test/CIR/Lowering/cast.cir

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 02360fb08cc79..cf07fc4f0833a 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -625,12 +625,22 @@ LogicalResult cir::CastOp::verify() {
                               "address space of the operand";
     }
 
-  if (mlir::isa<cir::VectorType>(srcType) &&
-      mlir::isa<cir::VectorType>(resType)) {
+  cir::CastKind kind = getKind();
+  auto srcVTy = mlir::dyn_cast<cir::VectorType>(srcType);
+  auto resVTy = mlir::dyn_cast<cir::VectorType>(resType);
+  if (srcVTy && resVTy) {
+    if ((kind == cir::CastKind::int_to_float ||
+         kind == cir::CastKind::float_to_int) &&
+        srcVTy.getSize() != resVTy.getSize()) {
+      return emitOpError()
+             << "vector float-to-int and int-to-float casts require "
+                "source and destination vectors to have the same number of "
+                "elements";
+    }
     // Use the element type of the vector to verify the cast kind. (Except for
     // bitcast, see below.)
-    srcType = mlir::dyn_cast<cir::VectorType>(srcType).getElementType();
-    resType = mlir::dyn_cast<cir::VectorType>(resType).getElementType();
+    srcType = srcVTy.getElementType();
+    resType = resVTy.getElementType();
   }
 
   switch (getKind()) {

diff  --git a/clang/test/CIR/IR/invalid-cast.cir 
b/clang/test/CIR/IR/invalid-cast.cir
index 2ceb77b5e1d41..cbe4354641497 100644
--- a/clang/test/CIR/IR/invalid-cast.cir
+++ b/clang/test/CIR/IR/invalid-cast.cir
@@ -25,3 +25,15 @@ module {
     cir.return %0 : !cir.bool
   }
 }
+
+// -----
+
+!s8i = !cir.int<s, 8>
+
+module {
+  cir.func @int_to_float(%in: !cir.vector<8 x !s8i>) {
+    // expected-error@+1 {{vector float-to-int and int-to-float casts require 
source and destination vectors to have the same number of elements}}
+    cir.cast int_to_float %in : !cir.vector<8 x !s8i> -> !cir.vector<2 x 
!cir.float>
+    cir.return
+  }
+}

diff  --git a/clang/test/CIR/Lowering/cast.cir 
b/clang/test/CIR/Lowering/cast.cir
index ec104edec2405..19ed51e14519f 100644
--- a/clang/test/CIR/Lowering/cast.cir
+++ b/clang/test/CIR/Lowering/cast.cir
@@ -92,4 +92,15 @@ module {
     cir.store %3, %1 : !u8i, !cir.ptr<!u8i>
     cir.return
   }
+
+  cir.func @vectorCasts(%arg0: !cir.vector<2 x !s32i>, %arg1: !cir.vector<4 x 
!cir.float>) {
+    %i_2_f = cir.cast int_to_float %arg0 : !cir.vector<2 x !s32i> -> 
!cir.vector<2 x !cir.float>
+    // CHECK: %{{.+}} = llvm.sitofp %{{.+}} : vector<2xi32> to vector<2xf32>
+    %f_2_1 = cir.cast float_to_int %arg1 : !cir.vector<4 x !cir.float> -> 
!cir.vector<4 x !s32i>
+    // CHECK: %{{.+}} = llvm.fptosi %{{.+}} : vector<4xf32> to vector<4xi32>
+    %bitcast = cir.cast bitcast %arg0 : !cir.vector<2 x !s32i> -> 
!cir.vector<1 x !cir.double>
+    // CHECK: %{{.+}} = llvm.bitcast %{{.+}} : vector<2xi32> to vector<1xf64>
+
+    cir.return
+  }
 }


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

Reply via email to