From 93cda6d6a75e98d5516fbf12ce984604be834f01 Mon Sep 17 00:00:00 2001 From: Vyacheslav Levytskyy Date: Tue, 5 Nov 2024 11:25:58 +0100 Subject: [PATCH] [SPIR-V] No OpBitcast is generated for a bitcast between identical types (#114877) The goal of the PR is to ensure that no OpBitcast is generated for a bitcast between identical types. This PR resolves https://github.com/llvm/llvm-project/issues/114482 --- llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp | 11 +++++++---- .../no-opbitcast-between-identical-types.ll | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 llvm/test/CodeGen/SPIRV/no-opbitcast-between-identical-types.ll diff --git a/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp b/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp index 790d86f191fd86..d5299043d9ef2f 100644 --- a/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp @@ -175,10 +175,13 @@ static void buildOpBitcast(SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, MachineRegisterInfo *MRI = MIB.getMRI(); if (!MRI->getRegClassOrNull(ResVReg)) MRI->setRegClass(ResVReg, GR->getRegClass(ResType)); - MIB.buildInstr(SPIRV::OpBitcast) - .addDef(ResVReg) - .addUse(GR->getSPIRVTypeID(ResType)) - .addUse(OpReg); + if (ResType == OpType) + MIB.buildInstr(TargetOpcode::COPY).addDef(ResVReg).addUse(OpReg); + else + MIB.buildInstr(SPIRV::OpBitcast) + .addDef(ResVReg) + .addUse(GR->getSPIRVTypeID(ResType)) + .addUse(OpReg); } // We do instruction selections early instead of calling MIB.buildBitcast() diff --git a/llvm/test/CodeGen/SPIRV/no-opbitcast-between-identical-types.ll b/llvm/test/CodeGen/SPIRV/no-opbitcast-between-identical-types.ll new file mode 100644 index 00000000000000..9b19a32d38f751 --- /dev/null +++ b/llvm/test/CodeGen/SPIRV/no-opbitcast-between-identical-types.ll @@ -0,0 +1,17 @@ +; The goal of the test case is to ensure that no OpBitcast is generated for a bitcast between identical types. + +; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s +; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %} + +; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s +; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %} + +; CHECK: OpFunction +; CHECK-NO: OpBitcast +; CHECK: OpReturn + +define void @foo() { +entry: + %r = bitcast i32 0 to i32 + ret void +}