Skip to content

Commit e3e0613

Browse files
[SPIR-V] Ensure that we don't have a dangling BlockAddress constants after internal intrinsic 'spv_switch' is processed (#92390)
After internal intrinsic 'spv_switch' is processed we need to delete G_BLOCK_ADDR instructions that were generated to keep track of the corresponding basic blocks. If we just delete G_BLOCK_ADDR instructions with BlockAddress operands, this leaves their BasicBlock counterparts in a "address taken" status. This would make AsmPrinter to generate a series of unneeded labels of a `"Address of block that was removed by CodeGen"` kind. This PR is to ensure that we don't have a dangling BlockAddress constants by zapping the BlockAddress nodes, and only after that proceed with erasing G_BLOCK_ADDR instructions. See also #87823 for more details.
1 parent 2ed8ff3 commit e3e0613

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,25 @@ static void processSwitches(MachineFunction &MF, SPIRVGlobalRegistry *GR,
602602
ToEraseMI.insert(Next);
603603
}
604604
}
605-
for (MachineInstr *BlockAddrI : ToEraseMI)
605+
606+
// If we just delete G_BLOCK_ADDR instructions with BlockAddress operands,
607+
// this leaves their BasicBlock counterparts in a "address taken" status. This
608+
// would make AsmPrinter to generate a series of unneeded labels of a "Address
609+
// of block that was removed by CodeGen" kind. Let's first ensure that we
610+
// don't have a dangling BlockAddress constants by zapping the BlockAddress
611+
// nodes, and only after that proceed with erasing G_BLOCK_ADDR instructions.
612+
Constant *Replacement =
613+
ConstantInt::get(Type::getInt32Ty(MF.getFunction().getContext()), 1);
614+
for (MachineInstr *BlockAddrI : ToEraseMI) {
615+
if (BlockAddrI->getOpcode() == TargetOpcode::G_BLOCK_ADDR) {
616+
BlockAddress *BA = const_cast<BlockAddress *>(
617+
BlockAddrI->getOperand(1).getBlockAddress());
618+
BA->replaceAllUsesWith(
619+
ConstantExpr::getIntToPtr(Replacement, BA->getType()));
620+
BA->destroyConstant();
621+
}
606622
BlockAddrI->eraseFromParent();
623+
}
607624
}
608625

609626
static bool isImplicitFallthrough(MachineBasicBlock &MBB) {

llvm/test/CodeGen/SPIRV/branching/switch-range-check.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
22
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv32-unknown-unknown %s -o - -filetype=obj | spirv-val %}
33

4+
; CHECK: OpFunction
45
; CHECK: %[[#Var:]] = OpPhi
56
; CHECK: OpSwitch %[[#Var]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]]
67
; CHECK-COUNT-11: OpBranch
78
; CHECK-NOT: OpBranch
9+
; CHECK: OpReturn
10+
; CHECK-NEXT: OpFunctionEnd
811

912
define spir_func void @foo(i64 noundef %addr, i64 noundef %as) {
1013
entry:

0 commit comments

Comments
 (0)