Skip to content

Commit a0dcbe4

Browse files
committed
llvm-reduce: Add reduction pass to remove regalloc hints
I'm a bit confused by what's actually stored for the allocation hints. The MIR parser only handles the "simple" case where there's a single hint. I don't really understand the assertion in clearSimpleHint, or under what circumstances there are multiple hint registers.
1 parent 2011052 commit a0dcbe4

File tree

7 files changed

+120
-2
lines changed

7 files changed

+120
-2
lines changed

llvm/test/tools/llvm-reduce/mir/preserve-reg-hints.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 -mtriple=amdgcn-amd-amdhsa --delta-passes=instructions --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
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# REQUIRES: amdgpu-registered-target
2+
# RUN: llvm-reduce -simplify-mir --delta-passes=register-hints -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 --check-prefix=RESULT %s < %t
4+
5+
# CHECK-INTERESTINGNESS: - { id: 0, class: vgpr_32, preferred-register: '$vgpr0' }
6+
# CHECK-INTERESTINGNESS: - { id: 2, class: vgpr_32, preferred-register: '%1' }
7+
# CHECK-INTERESTINGNESS-COUNT-5: V_MOV_B32
8+
9+
# RESULT: registers:
10+
# RESULT-NEXT: - { id: 0, class: vgpr_32, preferred-register: '$vgpr0' }
11+
# RESULT-NEXT: - { id: 1, class: vgpr_32 }
12+
# RESULT-NEXT: - { id: 2, class: vgpr_32, preferred-register: '%1' }
13+
# RESULT-NEXT: - { id: 3, class: vgpr_32 }
14+
# RESULT-NEXT: - { id: 4, class: vgpr_32 }
15+
16+
---
17+
name: func
18+
tracksRegLiveness: true
19+
registers:
20+
- { id: 0, class: vgpr_32, preferred-register: '$vgpr0' }
21+
- { id: 1, class: vgpr_32, preferred-register: '' }
22+
- { id: 2, class: vgpr_32, preferred-register: '%1' }
23+
- { id: 3, class: vgpr_32, preferred-register: '%4' }
24+
- { id: 4, class: vgpr_32, preferred-register: '%3' }
25+
body: |
26+
bb.0:
27+
liveins: $vgpr0, $vgpr1
28+
29+
S_WAITCNT 0
30+
%0:vgpr_32 = V_MOV_B32_e32 0, implicit $exec
31+
%1:vgpr_32 = V_MOV_B32_e32 1, implicit $exec
32+
%2:vgpr_32 = V_MOV_B32_e32 2, implicit $exec
33+
%3:vgpr_32 = V_MOV_B32_e32 3, implicit $exec
34+
%4:vgpr_32 = V_MOV_B32_e32 4, implicit $exec
35+
S_NOP 0
36+
S_ENDPGM 0, implicit %0, implicit %1, implicit %2, implicit %3, implicit %4
37+
...
38+

llvm/tools/llvm-reduce/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ add_llvm_tool(llvm-reduce
4141
deltas/ReduceInstructionsMIR.cpp
4242
deltas/ReduceInstructionFlagsMIR.cpp
4343
deltas/ReduceIRReferences.cpp
44+
deltas/ReduceVirtualRegisters.cpp
4445
llvm-reduce.cpp
4546

4647
DEPENDS

llvm/tools/llvm-reduce/DeltaManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "deltas/ReduceOperandsSkip.h"
3636
#include "deltas/ReduceOperandsToArgs.h"
3737
#include "deltas/ReduceSpecialGlobals.h"
38+
#include "deltas/ReduceVirtualRegisters.h"
3839
#include "llvm/Support/CommandLine.h"
3940

4041
using namespace llvm;
@@ -74,7 +75,8 @@ static cl::opt<std::string>
7475
reduceIRInstructionReferencesDeltaPass) \
7576
DELTA_PASS("ir-block-references", reduceIRBlockReferencesDeltaPass) \
7677
DELTA_PASS("ir-function-references", reduceIRFunctionReferencesDeltaPass) \
77-
DELTA_PASS("instruction-flags", reduceInstructionFlagsMIRDeltaPass)
78+
DELTA_PASS("instruction-flags", reduceInstructionFlagsMIRDeltaPass) \
79+
DELTA_PASS("register-hints", reduceVirtualRegisterHintsDeltaPass)
7880

7981
static void runAllDeltaPasses(TestRunner &Tester) {
8082
#define DELTA_PASS(NAME, FUNC) FUNC(Tester);

llvm/tools/llvm-reduce/ReducerWorkItem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,12 @@ static uint64_t computeMIRComplexityScoreImpl(const MachineFunction &MF) {
496496
// Add in the block count.
497497
Score += 2 * MF.size();
498498

499+
const MachineRegisterInfo &MRI = MF.getRegInfo();
500+
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
501+
Register Reg = Register::index2VirtReg(I);
502+
Score += MRI.getRegAllocationHints(Reg).second.size();
503+
}
504+
499505
for (const MachineBasicBlock &MBB : MF) {
500506
for (const MachineInstr &MI : MBB) {
501507
const unsigned Opc = MI.getOpcode();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===- ReduceVirtualRegisters.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 simplify virtual registers in MIR.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "ReduceVirtualRegisters.h"
15+
#include "Delta.h"
16+
#include "llvm/CodeGen/MachineRegisterInfo.h"
17+
18+
using namespace llvm;
19+
20+
static void dropRegisterHintsFromFunction(Oracle &O, MachineFunction &MF) {
21+
MachineRegisterInfo &MRI = MF.getRegInfo();
22+
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
23+
Register Reg = Register::index2VirtReg(I);
24+
25+
const std::pair<Register, SmallVector<Register, 4>> &Hints =
26+
MRI.getRegAllocationHints(Reg);
27+
if (Hints.second.empty())
28+
continue;
29+
30+
if (!O.shouldKeep())
31+
MRI.clearSimpleHint(Reg);
32+
}
33+
}
34+
35+
static void dropRegisterHintsFromFunctions(Oracle &O,
36+
ReducerWorkItem &WorkItem) {
37+
for (const Function &F : WorkItem.getModule()) {
38+
if (auto *MF = WorkItem.MMI->getMachineFunction(F))
39+
dropRegisterHintsFromFunction(O, *MF);
40+
}
41+
}
42+
43+
void llvm::reduceVirtualRegisterHintsDeltaPass(TestRunner &Test) {
44+
outs() << "*** Reducing virtual register hints from functions...\n";
45+
runDeltaPass(Test, dropRegisterHintsFromFunctions);
46+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===- ReduceVirtualRegisters.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 simplify virtual register information.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEVIRTUALREGISTERS_H
15+
#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEVIRTUALREGISTERS_H
16+
17+
namespace llvm {
18+
class TestRunner;
19+
20+
/// Remove register allocation hints from virtual registes.
21+
void reduceVirtualRegisterHintsDeltaPass(TestRunner &Test);
22+
23+
} // namespace llvm
24+
25+
#endif

0 commit comments

Comments
 (0)