Skip to content

Commit 47c8ec8

Browse files
committed
llvm-reduce: Add pass to remove register uses
Try to delete implicit uses, and add undef flags to explicit ones.
1 parent 81edc83 commit 47c8ec8

12 files changed

+159
-7
lines changed

llvm/test/tools/llvm-reduce/mir/generic-vreg.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# RESULT-NEXT: %add:_(s64) = G_ADD %aoeu, %aoeu
2323
# RESULT-NEXT: %ptr:_(p1) = G_IMPLICIT_DEF
2424
# RESULT-NEXT: G_STORE %{{[0-9]+}}(s32), %ptr(p1) :: (store (s32), addrspace 1)
25-
# RESULT-NEXT: S_ENDPGM 0, implicit %add(s64), implicit %{{[0-9]+}}(<2 x s16>), implicit %{{[0-9]+}}(s64)
25+
# RESULT-NEXT: S_ENDPGM 0{{$}}
2626

2727
---
2828
name: f

llvm/test/tools/llvm-reduce/mir/instr-reduce.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: llvm-reduce -mtriple=riscv32 --test %python --test-arg %p/instr-reduce.py %s -o %t
1+
# RUN: llvm-reduce --delta-passes=instructions -mtriple=riscv32 --test %python --test-arg %p/instr-reduce.py %s -o %t
22
# RUN: cat %t | FileCheck --match-full-lines %s
33

44
# REQUIRES: riscv-registered-target

llvm/test/tools/llvm-reduce/mir/preserve-machine-function-info-riscv.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# REQUIRES: riscv-registered-target
2-
# RUN: llvm-reduce -simplify-mir -mtriple=riscv64-- --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
2+
# RUN: llvm-reduce -simplify-mir --delta-passes=instructions -mtriple=riscv64-- --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
33
# RUN: FileCheck --check-prefix=RESULT %s < %t
44

55
# CHECK-INTERESTINGNESS: ADDW

llvm/test/tools/llvm-reduce/mir/reduce-instruction-flags.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# REQUIRES: amdgpu-registered-target
2-
# RUN: llvm-reduce -simplify-mir -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
2+
# RUN: llvm-reduce -simplify-mir --delta-passes=instruction-flags -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
33
# RUN: FileCheck --check-prefix=RESULT %s < %t
44

55
# CHECK-INTERESTINGNESS: V_ADD_F32
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# REQUIRES: amdgpu-registered-target
2+
# RUN: llvm-reduce -abort-on-invalid-reduction -simplify-mir --delta-passes=register-uses -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
3+
# RUN: FileCheck --match-full-lines --check-prefix=RESULT %s < %t
4+
5+
# CHECK-INTERESTINGNESS: V_MUL_F32_e32 %vgpr0
6+
# CHECK-INTERESTINGNESS: V_MUL_F32_e32 {{.*}}%vgpr1, %vgpr0
7+
8+
# CHECK-INTERESTINGNESS: SI_CALL {{.*}}$vgpr0
9+
# CHECK-INTERESTINGNESS: SI_CALL {{.*}}$vgpr3
10+
11+
12+
# RESULT: %mul0:vgpr_32 = V_MUL_F32_e32 %vgpr0, undef %vgpr1, implicit $mode, implicit $exec
13+
# RESULT: %mul1:vgpr_32 = V_MUL_F32_e32 undef %vgpr1, %vgpr0, implicit $mode, implicit $exec
14+
# RESULT: $vgpr0 = V_MOV_B32_e32 0, implicit $exec
15+
# RESULT: $vgpr1 = V_MOV_B32_e32 1, implicit $exec
16+
# RESULT: $sgpr30_sgpr31 = SI_CALL undef %call_target, @callee, csr_amdgpu, implicit $vgpr0
17+
# RESULT: $sgpr30_sgpr31 = SI_CALL undef %call_target, @callee, csr_amdgpu, implicit $vgpr3
18+
# RESULT: %impdef:vreg_64 = IMPLICIT_DEF
19+
# RESULT: GLOBAL_STORE_DWORD undef %ptr, undef %impdef.sub1, 0, 0, implicit $exec
20+
# RESULT: undef %impdef.sub0:vreg_64 = V_MOV_B32_e32 0, implicit $exec
21+
# RESULT: S_CBRANCH_SCC1 %bb.1, implicit undef $scc
22+
# RESULT: S_BARRIER
23+
# RESULT: S_ENDPGM 0
24+
25+
--- |
26+
define void @func() {
27+
ret void
28+
}
29+
30+
declare void @callee()
31+
...
32+
33+
---
34+
name: func
35+
tracksRegLiveness: true
36+
body: |
37+
bb.0:
38+
liveins: $vgpr0, $vgpr1
39+
40+
S_WAITCNT 0
41+
%vgpr0:vgpr_32 = COPY $vgpr0
42+
%vgpr1:vgpr_32 = COPY $vgpr1
43+
%mul0:vgpr_32 = V_MUL_F32_e32 %vgpr0, %vgpr1, implicit $mode, implicit $exec
44+
%mul1:vgpr_32 = V_MUL_F32_e32 %vgpr1, %vgpr0, implicit $mode, implicit $exec
45+
%call_target:sreg_64 = IMPLICIT_DEF
46+
$vgpr0 = V_MOV_B32_e32 0, implicit $exec
47+
$vgpr1 = V_MOV_B32_e32 1, implicit $exec
48+
$sgpr30_sgpr31 = SI_CALL %call_target, @callee, csr_amdgpu, implicit $vgpr0, implicit $vgpr1
49+
$vgpr2 = V_MOV_B32_e32 2, implicit $exec
50+
$vgpr3 = V_MOV_B32_e32 3, implicit $exec
51+
$sgpr30_sgpr31 = SI_CALL %call_target, @callee, csr_amdgpu, implicit $vgpr2, implicit $vgpr3
52+
%impdef:vreg_64 = IMPLICIT_DEF
53+
%ptr:vreg_64 = IMPLICIT_DEF
54+
GLOBAL_STORE_DWORD %ptr, %impdef.sub1, 0, 0, implicit $exec
55+
%impdef.sub0 = V_MOV_B32_e32 0, implicit $exec
56+
$scc = IMPLICIT_DEF
57+
S_CBRANCH_SCC1 %bb.1, implicit $scc
58+
59+
bb.1:
60+
S_BARRIER
61+
S_ENDPGM 0, implicit %mul0, implicit %mul1
62+
...
63+

llvm/test/tools/llvm-reduce/mir/subreg-def0.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# REQUIRES: amdgpu-registered-target
2-
# RUN: llvm-reduce -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
2+
# RUN: llvm-reduce --delta-passes=instructions -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
33
# RUN: FileCheck --match-full-lines --check-prefix=RESULT %s < %t
44

55
# CHECK-INTERESTINGNESS: V_ADD_U32

llvm/test/tools/llvm-reduce/mir/subreg-def1.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# REQUIRES: amdgpu-registered-target
2-
# RUN: llvm-reduce -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
2+
# RUN: llvm-reduce --delta-passes=instructions -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
33
# RUN: FileCheck --match-full-lines --check-prefix=RESULT %s < %t
44

55
# CHECK-INTERESTINGNESS: %{{[0-9]+}}.sub0:vreg_64 = V_ADD_U32_e32 %{{[0-9]+}}.sub1, %{{[0-9]+}}.sub0, implicit $exec

llvm/test/tools/llvm-reduce/mir/undef-virt-reg.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# REQUIRES: amdgpu-registered-target
2-
# RUN: llvm-reduce -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
2+
# RUN: llvm-reduce --delta-passes=instructions -mtriple=amdgcn-amd-amdhsa --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t 2> %t.log
33
# RUN: FileCheck --match-full-lines --check-prefix=RESULT %s < %t
44

55
# CHECK-INTERESTINGNESS: S_NOP 0

llvm/tools/llvm-reduce/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_llvm_tool(llvm-reduce
4242
deltas/ReduceInstructionFlagsMIR.cpp
4343
deltas/ReduceIRReferences.cpp
4444
deltas/ReduceVirtualRegisters.cpp
45+
deltas/ReduceRegisterUses.cpp
4546
llvm-reduce.cpp
4647

4748
DEPENDS

llvm/tools/llvm-reduce/DeltaManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "deltas/ReduceOperands.h"
3636
#include "deltas/ReduceOperandsSkip.h"
3737
#include "deltas/ReduceOperandsToArgs.h"
38+
#include "deltas/ReduceRegisterUses.h"
3839
#include "deltas/ReduceSpecialGlobals.h"
3940
#include "deltas/ReduceVirtualRegisters.h"
4041
#include "llvm/Support/CommandLine.h"
@@ -77,6 +78,7 @@ static cl::opt<std::string>
7778
DELTA_PASS("ir-block-references", reduceIRBlockReferencesDeltaPass) \
7879
DELTA_PASS("ir-function-references", reduceIRFunctionReferencesDeltaPass) \
7980
DELTA_PASS("instruction-flags", reduceInstructionFlagsMIRDeltaPass) \
81+
DELTA_PASS("register-uses", reduceRegisterUsesMIRDeltaPass) \
8082
DELTA_PASS("register-hints", reduceVirtualRegisterHintsDeltaPass)
8183

8284
static void runAllDeltaPasses(TestRunner &Tester) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===- ReduceRegisterUses.cpp - Specialized Delta Pass --------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements a function which calls the Generic Delta pass in order
10+
// to reduce uninteresting register uses from the MachineFunction.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "ReduceRegisterUses.h"
15+
#include "llvm/CodeGen/MachineFunction.h"
16+
#include "llvm/CodeGen/MachineRegisterInfo.h"
17+
18+
using namespace llvm;
19+
20+
static void removeUsesFromFunction(Oracle &O, MachineFunction &MF) {
21+
MachineRegisterInfo &MRI = MF.getRegInfo();
22+
23+
for (MachineBasicBlock &MBB : MF) {
24+
for (MachineInstr &MI : MBB) {
25+
int NumOperands = MI.getNumOperands();
26+
int NumRequiredOps = MI.getNumExplicitOperands() +
27+
MI.getDesc().getNumImplicitDefs() +
28+
MI.getDesc().getNumImplicitUses();
29+
30+
for (int I = NumOperands - 1; I >= 0; --I) {
31+
MachineOperand &MO = MI.getOperand(I);
32+
if (!MO.isReg() || !MO.readsReg())
33+
continue;
34+
35+
Register Reg = MO.getReg();
36+
if (Reg.isPhysical() && MRI.isReserved(Reg))
37+
continue;
38+
39+
if (O.shouldKeep())
40+
continue;
41+
42+
// Remove implicit operands. If the register is part of the fixed
43+
// operand list, set to undef.
44+
if (I >= NumRequiredOps)
45+
MI.removeOperand(I);
46+
else
47+
MO.setIsUndef();
48+
}
49+
}
50+
}
51+
}
52+
53+
static void removeUsesFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
54+
for (const Function &F : WorkItem.getModule()) {
55+
if (auto *MF = WorkItem.MMI->getMachineFunction(F))
56+
removeUsesFromFunction(O, *MF);
57+
}
58+
}
59+
60+
void llvm::reduceRegisterUsesMIRDeltaPass(TestRunner &Test) {
61+
outs() << "*** Reducing register uses...\n";
62+
runDeltaPass(Test, removeUsesFromModule);
63+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===- ReduceRegisterUses.h - Specialized Delta Pass -----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements a function which calls the Generic Delta pass in order
10+
// to reduce uninteresting register uses from the MachineFunction.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEREGISTERUSES_H
15+
#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEREGISTERUSES_H
16+
17+
#include "Delta.h"
18+
19+
namespace llvm {
20+
void reduceRegisterUsesMIRDeltaPass(TestRunner &Test);
21+
} // namespace llvm
22+
23+
#endif

0 commit comments

Comments
 (0)