Skip to content

Commit

Permalink
[SPIR-V] No OpBitcast is generated for a bitcast between identical ty…
Browse files Browse the repository at this point in the history
…pes (llvm#114877)

The goal of the PR is to ensure that no OpBitcast is generated for a
bitcast between identical types.

This PR resolves llvm#114482
  • Loading branch information
VyacheslavLevytskyy authored Nov 5, 2024
1 parent 5dc9c39 commit 93cda6d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 7 additions & 4 deletions llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions llvm/test/CodeGen/SPIRV/no-opbitcast-between-identical-types.ll
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 93cda6d

Please sign in to comment.