[Mlir-commits] [mlir] [mlir][LLVMIR] Skip or folding for mismatched constant types (PR #226026)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Sep 24 00:57:40 PDT 2026
https://github.com/AkunCube updated https://github.com/llvm/llvm-project/pull/226026
>From 473a2d1ac56110813109b295078aada279b6d2d2 Mon Sep 17 00:00:00 2001
From: cubehan3 <cubehan3 at gmail.com>
Date: Thu, 24 Sep 2026 05:34:46 +0000
Subject: [PATCH 1/2] [mlir][LLVMIR] Skip or folding for mismatched constant
types
---
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp | 4 +--
.../test/Dialect/LLVMIR/constant-folding.mlir | 30 +++++++++++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index b931631369e6f..74d6934307559 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -4148,11 +4148,11 @@ OpFoldResult LLVM::ShlOp::fold(FoldAdaptor adaptor) {
OpFoldResult LLVM::OrOp::fold(FoldAdaptor adaptor) {
auto lhs = dyn_cast_or_null<IntegerAttr>(adaptor.getLhs());
- if (!lhs)
+ if (!lhs || lhs.getType() != getType())
return {};
auto rhs = dyn_cast_or_null<IntegerAttr>(adaptor.getRhs());
- if (!rhs)
+ if (!rhs || rhs.getType() != getType())
return {};
return IntegerAttr::get(getType(), lhs.getValue() | rhs.getValue());
diff --git a/mlir/test/Dialect/LLVMIR/constant-folding.mlir b/mlir/test/Dialect/LLVMIR/constant-folding.mlir
index 0a6227bd2607b..cf5f062a6fffa 100644
--- a/mlir/test/Dialect/LLVMIR/constant-folding.mlir
+++ b/mlir/test/Dialect/LLVMIR/constant-folding.mlir
@@ -88,6 +88,36 @@ llvm.func @or_basic() -> i32 {
// -----
+// A constant-like op can fold to an attribute whose type differs from its
+// result type. Do not fold llvm.or using such an attribute.
+// CHECK-LABEL: llvm.func @or_mismatched_lhs
+llvm.func @or_mismatched_lhs() -> i32 {
+ // CHECK: %[[LHS:.*]] = "test.constant"() <{value = 1 : i64}> : () -> i32
+ %lhs = "test.constant"() {value = 1 : i64} : () -> i32
+ // CHECK: %[[RHS:.*]] = "test.constant"() <{value = 2 : i32}> : () -> i32
+ %rhs = "test.constant"() {value = 2 : i32} : () -> i32
+ // CHECK: %[[RESULT:.*]] = llvm.or %[[LHS]], %[[RHS]] : i32
+ %result = llvm.or %lhs, %rhs : i32
+ // CHECK: llvm.return %[[RESULT]] : i32
+ llvm.return %result : i32
+}
+
+// -----
+
+// CHECK-LABEL: llvm.func @or_mismatched_rhs
+llvm.func @or_mismatched_rhs() -> i32 {
+ // CHECK: %[[LHS:.*]] = "test.constant"() <{value = 1 : i32}> : () -> i32
+ %lhs = "test.constant"() {value = 1 : i32} : () -> i32
+ // CHECK: %[[RHS:.*]] = "test.constant"() <{value = 2 : i64}> : () -> i32
+ %rhs = "test.constant"() {value = 2 : i64} : () -> i32
+ // CHECK: %[[RESULT:.*]] = llvm.or %[[LHS]], %[[RHS]] : i32
+ %result = llvm.or %lhs, %rhs : i32
+ // CHECK: llvm.return %[[RESULT]] : i32
+ llvm.return %result : i32
+}
+
+// -----
+
// CHECK-LABEL: llvm.func @addressof
llvm.func @addressof() {
// CHECK-NEXT: %[[ADDRESSOF:.+]] = llvm.mlir.addressof @foo
>From 85020a403755bc85aa15555c598ec595af9b0e31 Mon Sep 17 00:00:00 2001
From: cubehan3 <cubehan3 at gmail.com>
Date: Thu, 24 Sep 2026 07:56:49 +0000
Subject: [PATCH 2/2] [mlir][LLVMIR] Guard shl folding against mismatched
constant types
---
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp | 8 +++--
.../test/Dialect/LLVMIR/constant-folding.mlir | 32 +++++++++++++++++++
2 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 74d6934307559..21da3cef97fbd 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -4128,15 +4128,17 @@ Value LLVM::GEPOp::getViewSource() { return getBase(); }
//===----------------------------------------------------------------------===//
OpFoldResult LLVM::ShlOp::fold(FoldAdaptor adaptor) {
+ // A constant-like op may fold to an attribute whose type differs from its
+ // result type.
auto rhs = dyn_cast_or_null<IntegerAttr>(adaptor.getRhs());
- if (!rhs)
+ if (!rhs || rhs.getType() != getType())
return {};
if (rhs.getValue().uge(getLhs().getType().getIntOrFloatBitWidth()))
return {}; // TODO: Fold into poison.
auto lhs = dyn_cast_or_null<IntegerAttr>(adaptor.getLhs());
- if (!lhs)
+ if (!lhs || lhs.getType() != getType())
return {};
return IntegerAttr::get(getType(), lhs.getValue().shl(rhs.getValue()));
@@ -4147,6 +4149,8 @@ OpFoldResult LLVM::ShlOp::fold(FoldAdaptor adaptor) {
//===----------------------------------------------------------------------===//
OpFoldResult LLVM::OrOp::fold(FoldAdaptor adaptor) {
+ // A constant-like op may fold to an attribute whose type differs from its
+ // result type.
auto lhs = dyn_cast_or_null<IntegerAttr>(adaptor.getLhs());
if (!lhs || lhs.getType() != getType())
return {};
diff --git a/mlir/test/Dialect/LLVMIR/constant-folding.mlir b/mlir/test/Dialect/LLVMIR/constant-folding.mlir
index cf5f062a6fffa..5e2256a1dd5d5 100644
--- a/mlir/test/Dialect/LLVMIR/constant-folding.mlir
+++ b/mlir/test/Dialect/LLVMIR/constant-folding.mlir
@@ -76,6 +76,37 @@ llvm.func @shl_largest_valid_shift() -> i128 {
// -----
+// A constant-like op can fold to an attribute whose type differs from its
+// result type. Do not fold llvm.shl using such an attribute.
+
+// CHECK-LABEL: llvm.func @shl_mismatched_lhs
+llvm.func @shl_mismatched_lhs() -> i32 {
+ // CHECK: %[[LHS:.*]] = "test.constant"() <{value = 1 : i64}> : () -> i32
+ %lhs = "test.constant"() {value = 1 : i64} : () -> i32
+ // CHECK: %[[RHS:.*]] = "test.constant"() <{value = 2 : i32}> : () -> i32
+ %rhs = "test.constant"() {value = 2 : i32} : () -> i32
+ // CHECK: %[[RESULT:.*]] = llvm.shl %[[LHS]], %[[RHS]] : i32
+ %result = llvm.shl %lhs, %rhs : i32
+ // CHECK: llvm.return %[[RESULT]] : i32
+ llvm.return %result : i32
+}
+
+// -----
+
+// CHECK-LABEL: llvm.func @shl_mismatched_rhs
+llvm.func @shl_mismatched_rhs() -> i32 {
+ // CHECK: %[[LHS:.*]] = "test.constant"() <{value = 1 : i32}> : () -> i32
+ %lhs = "test.constant"() {value = 1 : i32} : () -> i32
+ // CHECK: %[[RHS:.*]] = "test.constant"() <{value = 2 : i64}> : () -> i32
+ %rhs = "test.constant"() {value = 2 : i64} : () -> i32
+ // CHECK: %[[RESULT:.*]] = llvm.shl %[[LHS]], %[[RHS]] : i32
+ %result = llvm.shl %lhs, %rhs : i32
+ // CHECK: llvm.return %[[RESULT]] : i32
+ llvm.return %result : i32
+}
+
+// -----
+
// CHECK-LABEL: llvm.func @or_basic
llvm.func @or_basic() -> i32 {
%0 = llvm.mlir.constant(5 : i32) : i32
@@ -90,6 +121,7 @@ llvm.func @or_basic() -> i32 {
// A constant-like op can fold to an attribute whose type differs from its
// result type. Do not fold llvm.or using such an attribute.
+
// CHECK-LABEL: llvm.func @or_mismatched_lhs
llvm.func @or_mismatched_lhs() -> i32 {
// CHECK: %[[LHS:.*]] = "test.constant"() <{value = 1 : i64}> : () -> i32
More information about the Mlir-commits
mailing list