[llvm] [SPIR-V] Ensure that we don't have a dangling BlockAddress constants after internal intrinsic 'spv_switch' is processed (PR #92390)
Vyacheslav Levytskyy via llvm-commits
llvm-commits at lists.llvm.org
Thu May 16 05:14:14 PDT 2024
https://github.com/VyacheslavLevytskyy created https://github.com/llvm/llvm-project/pull/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.
>From 637fe58811a0a1ea99be93fa795b6d11fba67dea Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Thu, 16 May 2024 05:11:10 -0700
Subject: [PATCH] ensure that we don't have a dangling BlockAddress constants
after 'spv_switch'
---
llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp | 19 ++++++++++++++++++-
.../SPIRV/branching/switch-range-check.ll | 3 +++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp b/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp
index 6ee5d90d9afe1..9bff23dd96668 100644
--- a/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVPreLegalizer.cpp
@@ -602,8 +602,25 @@ static void processSwitches(MachineFunction &MF, SPIRVGlobalRegistry *GR,
ToEraseMI.insert(Next);
}
}
- for (MachineInstr *BlockAddrI : ToEraseMI)
+
+ // 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. Let's first 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.
+ Constant *Replacement =
+ ConstantInt::get(Type::getInt32Ty(MF.getFunction().getContext()), 1);
+ for (MachineInstr *BlockAddrI : ToEraseMI) {
+ if (BlockAddrI->getOpcode() == TargetOpcode::G_BLOCK_ADDR) {
+ BlockAddress *BA = const_cast<BlockAddress *>(
+ BlockAddrI->getOperand(1).getBlockAddress());
+ BA->replaceAllUsesWith(
+ ConstantExpr::getIntToPtr(Replacement, BA->getType()));
+ BA->destroyConstant();
+ }
BlockAddrI->eraseFromParent();
+ }
}
static bool isImplicitFallthrough(MachineBasicBlock &MBB) {
diff --git a/llvm/test/CodeGen/SPIRV/branching/switch-range-check.ll b/llvm/test/CodeGen/SPIRV/branching/switch-range-check.ll
index 85a4d4db089cb..f8ce15323aacf 100644
--- a/llvm/test/CodeGen/SPIRV/branching/switch-range-check.ll
+++ b/llvm/test/CodeGen/SPIRV/branching/switch-range-check.ll
@@ -1,10 +1,13 @@
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv32-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+; CHECK: OpFunction
; CHECK: %[[#Var:]] = OpPhi
; CHECK: OpSwitch %[[#Var]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]] [[#]] %[[#]]
; CHECK-COUNT-11: OpBranch
; CHECK-NOT: OpBranch
+; CHECK: OpReturn
+; CHECK-NEXT: OpFunctionEnd
define spir_func void @foo(i64 noundef %addr, i64 noundef %as) {
entry:
More information about the llvm-commits
mailing list