[llvm] [GISel] Add narrowScalar/widenScalar support for `G_CONSTANT_FOLD_BARRIER` (PR #93031)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed May 22 06:42:48 PDT 2024
https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/93031
Fixes an error that llc fails to legalize `G_CONSTANT_FOLD_BARRIER` with i16/i128: https://godbolt.org/z/f9n6xM3sv
>From 5d953fd71caa61e8e8e70f8233146f48c08659d0 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 22 May 2024 21:36:26 +0800
Subject: [PATCH] [GISel] Add narrowScalar/widenScalar support for
`G_CONSTANT_FOLD_BARRIER`
---
.../CodeGen/GlobalISel/LegalizerHelper.cpp | 6 +-
.../RISCV/GlobalISel/constbarrier-rv32.ll | 60 +++++++++++++++++++
.../RISCV/GlobalISel/constbarrier-rv64.ll | 36 +++++++++++
.../legalizer/legalize-constbarrier-rv32.mir | 32 ++++++++++
.../legalizer/legalize-constbarrier-rv64.mir | 33 ++++++++++
5 files changed, 165 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll
create mode 100644 llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll
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
More information about the llvm-commits
mailing list