[llvm] [GlobalISel] Have buildExtOrTrunc create G_BITCAST on mismatching types. (PR #192939)
David Green via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 20 04:08:08 PDT 2026
https://github.com/davemgreen created https://github.com/llvm/llvm-project/pull/192939
As it currently stands, instruction such as `%0:_(bf16) = G_TRUNC %1:_(i32)` and `%2:_(f32) = G_ANYEXT %0:_(bf16)` are accepted by GlobalISel. (They can be created from call lowering, in this case for Arm in #191830). A COPY from different types `%0:_(f32) = COPY %1:_(i32)` is not accepted though, leading to verifier errors.
This patch makes buildExtOrTrunc produce a G_BITCAST instead of a COPY for mismatching types. Alternatives could be that G_TRUNC / G_ANYEXT need to have integer types, or perhaps more preferably that COPY can have mismatching types so long as the (element) sizes match.
>From 40a7d49963d4243310cc3e654b1a417065243dac Mon Sep 17 00:00:00 2001
From: David Green <david.green at arm.com>
Date: Mon, 20 Apr 2026 11:56:37 +0100
Subject: [PATCH] [GlobalISel] Have buildExtOrTrunc create G_BITCAST on
mismatching types.
As it currently stands, instruction such as `%0:_(bf16) = G_TRUNC %1:_(i32)`
and `%2:_(f32) = G_ANYEXT %0:_(bf16)` are accepted by GlobalISel. (They cab be
created from call lowering, in this case for Arm). A COPY from different types
`%0:_(f32) = COPY %1:_(i32)` is not though.
This patch makes buildExtOrTrunc produce a G_BITCAST instead of a COPY for
mismatching types. Alternatives could be that G_TRUNC / G_ANYEXT need to have
integer types, or perhaps more preferably that COPY can have mismatching types
so long as the sizes match.
---
.../CodeGen/GlobalISel/MachineIRBuilder.cpp | 2 ++
.../GlobalISel/legalize-fptruncext.mir | 22 +++++++++++++++++++
2 files changed, 24 insertions(+)
create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/legalize-fptruncext.mir
diff --git a/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp b/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
index 63c9ada9b059d..f935a746bbcef 100644
--- a/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
@@ -586,6 +586,8 @@ MachineInstrBuilder MachineIRBuilder::buildExtOrTrunc(unsigned ExtOpc,
else if (Res.getLLTTy(*getMRI()).getSizeInBits() <
Op.getLLTTy(*getMRI()).getSizeInBits())
Opcode = TargetOpcode::G_TRUNC;
+ else if (Res.getLLTTy(*getMRI()) != Op.getLLTTy(*getMRI()))
+ Opcode = TargetOpcode::G_BITCAST;
else
assert(Res.getLLTTy(*getMRI()).getSizeInBits() ==
Op.getLLTTy(*getMRI()).getSizeInBits());
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/legalize-fptruncext.mir b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-fptruncext.mir
new file mode 100644
index 0000000000000..d74daaf27023b
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-fptruncext.mir
@@ -0,0 +1,22 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
+# RUN: llc -mtriple aarch64-unknown-unknown -run-pass=legalizer -global-isel -verify-machineinstrs %s -o - | FileCheck %s
+---
+name: trunc_anyext_s16_s32
+body: |
+ bb.0:
+ liveins: $w0
+ ; CHECK-LABEL: name: trunc_anyext_s16_s32
+ ; CHECK: liveins: $w0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(i32) = COPY $w0
+ ; CHECK-NEXT: [[BITCAST:%[0-9]+]]:_(f32) = G_BITCAST [[COPY]](i32)
+ ; CHECK-NEXT: $s0 = COPY [[BITCAST]](f32)
+ ; CHECK-NEXT: RET_ReallyLR implicit $s0
+ %1:_(i32) = COPY $w0
+ %0:_(bf16) = G_TRUNC %1:_(i32)
+ %2:_(f32) = G_ANYEXT %0:_(bf16)
+ $s0 = COPY %2:_(f32)
+ RET_ReallyLR implicit $s0
+...
+
+
More information about the llvm-commits
mailing list