-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[GISel] Add narrowScalar/widenScalar support for G_CONSTANT_FOLD_BARRIER
#93031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-globalisel Author: Yingwei Zheng (dtcxzyw) ChangesFixes an error that llc fails to legalize Full diff: https://github.com/llvm/llvm-project/pull/93031.diff 5 Files Affected:
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 40507845d8d89..d8b0f52ecf9e3 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -1296,7 +1296,7 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
MI.eraseFromParent();
return Legalized;
}
-
+ case TargetOpcode::G_CONSTANT_FOLD_BARRIER:
case TargetOpcode::G_FREEZE: {
if (TypeIdx != 0)
return UnableToLegalize;
@@ -1310,7 +1310,8 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
SmallVector<Register, 8> Parts;
for (unsigned i = 0; i < Unmerge->getNumDefs(); ++i) {
Parts.push_back(
- MIRBuilder.buildFreeze(NarrowTy, Unmerge.getReg(i)).getReg(0));
+ MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, {Unmerge.getReg(i)})
+ .getReg(0));
}
MIRBuilder.buildMergeLikeInstr(MI.getOperand(0).getReg(), Parts);
@@ -2515,6 +2516,7 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
return Legalized;
}
case TargetOpcode::G_FREEZE:
+ case TargetOpcode::G_CONSTANT_FOLD_BARRIER:
Observer.changingInstr(MI);
widenScalarSrc(MI, WideTy, 1, TargetOpcode::G_ANYEXT);
widenScalarDst(MI, WideTy);
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll b/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll
new file mode 100644
index 0000000000000..70d1b25309c84
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll
@@ -0,0 +1,60 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=riscv32 -global-isel -verify-machineinstrs < %s \
+; RUN: | FileCheck %s --check-prefixes=RV32
+
+define i16 @constant_fold_barrier_i16(i16 %x, i16 %y) {
+; RV32-LABEL: constant_fold_barrier_i16:
+; RV32: # %bb.0: # %entry
+; RV32-NEXT: li a1, 1
+; RV32-NEXT: slli a1, a1, 11
+; RV32-NEXT: and a0, a0, a1
+; RV32-NEXT: addi a1, a1, 289
+; RV32-NEXT: or a0, a0, a1
+; RV32-NEXT: ret
+entry:
+ %and = and i16 %x, 2048
+ %or = or i16 %and, 2337
+ ret i16 %or
+}
+
+define void @constant_fold_barrier_i128(ptr %p) {
+; RV32-LABEL: constant_fold_barrier_i128:
+; RV32: # %bb.0: # %entry
+; RV32-NEXT: li a1, 1
+; RV32-NEXT: slli a1, a1, 11
+; RV32-NEXT: lw a2, 0(a0)
+; RV32-NEXT: lw a3, 4(a0)
+; RV32-NEXT: lw a4, 8(a0)
+; RV32-NEXT: lw a5, 12(a0)
+; RV32-NEXT: and a2, a2, a1
+; RV32-NEXT: and a3, a3, zero
+; RV32-NEXT: and a4, a4, zero
+; RV32-NEXT: and a5, a5, zero
+; RV32-NEXT: add a2, a2, a1
+; RV32-NEXT: sltu a1, a2, a1
+; RV32-NEXT: add a6, a3, zero
+; RV32-NEXT: sltu a3, a6, a3
+; RV32-NEXT: add a6, a6, a1
+; RV32-NEXT: seqz a7, a6
+; RV32-NEXT: and a1, a7, a1
+; RV32-NEXT: or a1, a3, a1
+; RV32-NEXT: add a3, a4, zero
+; RV32-NEXT: sltu a4, a3, a4
+; RV32-NEXT: add a3, a3, a1
+; RV32-NEXT: seqz a7, a3
+; RV32-NEXT: and a1, a7, a1
+; RV32-NEXT: or a1, a4, a1
+; RV32-NEXT: add a5, a5, zero
+; RV32-NEXT: add a1, a5, a1
+; RV32-NEXT: sw a2, 0(a0)
+; RV32-NEXT: sw a6, 4(a0)
+; RV32-NEXT: sw a3, 8(a0)
+; RV32-NEXT: sw a1, 12(a0)
+; RV32-NEXT: ret
+entry:
+ %x = load i128, ptr %p
+ %and = and i128 %x, 2048
+ %add = add i128 %and, 2048
+ store i128 %add, ptr %p
+ ret void
+}
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll b/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll
new file mode 100644
index 0000000000000..21d7b1d70714d
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll
@@ -0,0 +1,36 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=riscv64 -global-isel -verify-machineinstrs < %s \
+; RUN: | FileCheck %s --check-prefixes=RV64
+
+define i16 @constant_fold_barrier_i16(i16 %x, i16 %y) {
+; RV64-LABEL: constant_fold_barrier_i16:
+; RV64: # %bb.0: # %entry
+; RV64-NEXT: li a1, 1
+; RV64-NEXT: slli a1, a1, 11
+; RV64-NEXT: and a0, a0, a1
+; RV64-NEXT: addiw a1, a1, 289
+; RV64-NEXT: or a0, a0, a1
+; RV64-NEXT: ret
+entry:
+ %and = and i16 %x, 2048
+ %or = or i16 %and, 2337
+ ret i16 %or
+}
+
+define i128 @constant_fold_barrier_i128(i128 %x) {
+; RV64-LABEL: constant_fold_barrier_i128:
+; RV64: # %bb.0: # %entry
+; RV64-NEXT: li a2, 1
+; RV64-NEXT: slli a2, a2, 11
+; RV64-NEXT: and a0, a0, a2
+; RV64-NEXT: and a1, a1, zero
+; RV64-NEXT: add a0, a0, a2
+; RV64-NEXT: sltu a2, a0, a2
+; RV64-NEXT: add a1, a1, zero
+; RV64-NEXT: add a1, a1, a2
+; RV64-NEXT: ret
+entry:
+ %and = and i128 %x, 2048
+ %add = add i128 %and, 2048
+ ret i128 %add
+}
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir
index 6b1fc2042e2b8..bbe8ef4b092d3 100644
--- a/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv32.mir
@@ -14,6 +14,38 @@ body: |
$x10 = COPY %2(s32)
PseudoRET implicit $x10
+...
+---
+name: constbarrier_i16
+body: |
+ bb.0.entry:
+ ; CHECK-LABEL: name: constbarrier_i16
+ ; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
+ ; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
+ ; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s32)
+ ; CHECK-NEXT: PseudoRET implicit $x10
+ %1:_(s16) = G_CONSTANT i16 2048
+ %2:_(s16) = G_CONSTANT_FOLD_BARRIER %1
+ %3:_(s32) = G_ANYEXT %2(s16)
+ $x10 = COPY %3(s32)
+ PseudoRET implicit $x10
+
+...
+---
+name: constbarrier_i128
+body: |
+ bb.0.entry:
+ ; CHECK-LABEL: name: constbarrier_i128
+ ; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
+ ; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
+ ; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s32)
+ ; CHECK-NEXT: PseudoRET implicit $x10
+ %1:_(s128) = G_CONSTANT i128 2048
+ %2:_(s128) = G_CONSTANT_FOLD_BARRIER %1
+ %3:_(s32) = G_TRUNC %2(s128)
+ $x10 = COPY %3(s32)
+ PseudoRET implicit $x10
+
...
---
name: constbarrier_nxv2i1
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir
index de6a82beee2ab..96b1aa53d46ea 100644
--- a/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/legalizer/legalize-constbarrier-rv64.mir
@@ -31,6 +31,39 @@ body: |
$x10 = COPY %2(s64)
PseudoRET implicit $x10
+...
+---
+name: constbarrier_i16
+body: |
+ bb.0.entry:
+ ; CHECK-LABEL: name: constbarrier_i16
+ ; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 2048
+ ; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s32) = G_CONSTANT_FOLD_BARRIER [[C]]
+ ; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[CONSTANT_FOLD_BARRIER]](s32)
+ ; CHECK-NEXT: $x10 = COPY [[ANYEXT]](s64)
+ ; CHECK-NEXT: PseudoRET implicit $x10
+ %1:_(s16) = G_CONSTANT i16 2048
+ %2:_(s16) = G_CONSTANT_FOLD_BARRIER %1
+ %3:_(s64) = G_ANYEXT %2(s16)
+ $x10 = COPY %3(s64)
+ PseudoRET implicit $x10
+
+...
+---
+name: constbarrier_i128
+body: |
+ bb.0.entry:
+ ; CHECK-LABEL: name: constbarrier_i128
+ ; CHECK: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 2048
+ ; CHECK-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:_(s64) = G_CONSTANT_FOLD_BARRIER [[C]]
+ ; CHECK-NEXT: $x10 = COPY [[CONSTANT_FOLD_BARRIER]](s64)
+ ; CHECK-NEXT: PseudoRET implicit $x10
+ %1:_(s128) = G_CONSTANT i128 2048
+ %2:_(s128) = G_CONSTANT_FOLD_BARRIER %1
+ %3:_(s64) = G_TRUNC %2(s128)
+ $x10 = COPY %3(s64)
+ PseudoRET implicit $x10
+
...
---
name: constbarrier_nxv2i1
|
arsenm
approved these changes
May 22, 2024
PhilippvK
pushed a commit
to PhilippvK/llvm-project
that referenced
this pull request
Jun 24, 2024
…RIER` (llvm#93031) Fixes an error that llc fails to legalize `G_CONSTANT_FOLD_BARRIER` with i16/i128: https://godbolt.org/z/f9n6xM3sv
PhilippvK
pushed a commit
to PhilippvK/llvm-project
that referenced
this pull request
Oct 7, 2024
…RIER` (llvm#93031) Fixes an error that llc fails to legalize `G_CONSTANT_FOLD_BARRIER` with i16/i128: https://godbolt.org/z/f9n6xM3sv
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes an error that llc fails to legalize
G_CONSTANT_FOLD_BARRIER
with i16/i128: https://godbolt.org/z/f9n6xM3sv