Skip to content

Commit dbb220a

Browse files
author
Martien de Jong
committed
[AIE] Use ConflictBits in HazardRecognizer conflict interfaces
1 parent c7b8e82 commit dbb220a

File tree

4 files changed

+124
-19
lines changed

4 files changed

+124
-19
lines changed

llvm/lib/Target/AIE/AIEHazardRecognizer.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void FuncUnitWrapper::setFormatInterface(const AIEBaseMCFormats *Formats) {
6161

6262
bool FuncUnitWrapper::operator==(const FuncUnitWrapper &Other) const {
6363
return Required == Other.Required && Reserved == Other.Reserved &&
64-
Slots == Other.Slots && MemoryBanks == Other.MemoryBanks &&
64+
Slots == Other.Slots && Conflicts == Other.Conflicts &&
65+
MemoryBanks == Other.MemoryBanks &&
6566
MemObjectsBits == Other.MemObjectsBits;
6667
}
6768

@@ -95,6 +96,7 @@ void FuncUnitWrapper::clearResources() {
9596
Required.clear();
9697
Reserved.clear();
9798
Slots = 0;
99+
Conflicts = 0;
98100
MemoryBanks = 0;
99101
MemObjectsBits = 0;
100102
}
@@ -117,6 +119,7 @@ FuncUnitWrapper &FuncUnitWrapper::operator|=(const FuncUnitWrapper &Other) {
117119
Required |= Other.Required;
118120
Reserved |= Other.Reserved;
119121
Slots |= Other.Slots;
122+
Conflicts |= Other.Conflicts;
120123
MemoryBanks |= Other.MemoryBanks;
121124
MemObjectsBits |= Other.MemObjectsBits;
122125
return *this;
@@ -125,6 +128,7 @@ FuncUnitWrapper &FuncUnitWrapper::operator|=(const FuncUnitWrapper &Other) {
125128
bool FuncUnitWrapper::conflict(const FuncUnitWrapper &Other) const {
126129
if ((Slots & Other.Slots) != 0 || (MemoryBanks & Other.MemoryBanks) != 0 ||
127130
(MemObjectsBits & Other.MemObjectsBits) != 0 ||
131+
(Conflicts & Other.Slots) != 0 || (Slots & Other.Conflicts) != 0 ||
128132
Required.overlap(Other.Required) || Reserved.overlap(Other.Required) ||
129133
Required.overlap(Other.Reserved)) {
130134

@@ -482,6 +486,15 @@ static SlotBits getSlotSet(const MCInstrDesc &Desc,
482486
return IgnoreUnkownSlotSets ? 0 : ~0;
483487
}
484488

489+
static SlotBits getConflictSet(const MCInstrDesc &Desc,
490+
const AIEBaseMCFormats &Formats) {
491+
MCSlotKind SlotKind = Formats.getSlotKind(Desc.getOpcode());
492+
if (SlotKind != MCSlotKind())
493+
return Formats.getSlotInfo(SlotKind)->getConflictSet();
494+
495+
return 0;
496+
}
497+
485498
namespace {
486499
auto toHazardType(bool Conflict) {
487500
return Conflict ? ScheduleHazardRecognizer::NoopHazard
@@ -525,8 +538,9 @@ ScheduleHazardRecognizer::HazardType AIEHazardRecognizer::getHazardType(
525538
return toHazardType(checkConflict(
526539
TheScoreboard, ItinData, SchedClass,
527540
getSlotSet(Desc, *TII->getFormatInterface(), IgnoreUnknownSlotSets),
528-
MemoryBanks, MemObjectsBits, TII->getMemoryCycles(SchedClass),
529-
DeltaCycles, FUDepthLimit));
541+
getConflictSet(Desc, *TII->getFormatInterface()), MemoryBanks,
542+
MemObjectsBits, TII->getMemoryCycles(SchedClass), DeltaCycles,
543+
FUDepthLimit));
530544
}
531545

532546
bool AIEHazardRecognizer::checkConflict(
@@ -540,25 +554,28 @@ bool AIEHazardRecognizer::checkConflict(
540554
return checkConflict(
541555
Scoreboard, ItinData, SchedClass,
542556
getSlotSet(Desc, *TII->getFormatInterface(), IgnoreUnknownSlotSets),
543-
MemoryBanks, MemObjectsBits, TII->getMemoryCycles(SchedClass),
544-
DeltaCycles, std::nullopt);
557+
getConflictSet(Desc, *TII->getFormatInterface()), MemoryBanks,
558+
MemObjectsBits, TII->getMemoryCycles(SchedClass), DeltaCycles,
559+
std::nullopt);
545560
}
546561

547562
bool AIEHazardRecognizer::checkConflict(
548563
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
549564
const InstrItineraryData *ItinData, unsigned SchedClass, SlotBits SlotSet,
550-
MemoryBankBits MemoryBanks, MemoryObjectsBits MemObjectsBits,
551-
SmallVector<int, 2> MemoryAccessCycles, int DeltaCycles,
552-
std::optional<int> FUDepthLimit) {
565+
SlotBits ConflictSet, MemoryBankBits MemoryBanks,
566+
MemoryObjectsBits MemObjectsBits, SmallVector<int, 2> MemoryAccessCycles,
567+
int DeltaCycles, std::optional<int> FUDepthLimit) {
553568

554569
// Verify format hazards
555-
FuncUnitWrapper EmissionCycle(SlotSet);
570+
FuncUnitWrapper EmissionCycle(SlotSet, ConflictSet);
556571
if (EmissionCycle.conflict(Scoreboard[DeltaCycles]))
557572
return true;
558573

559574
// Verify memory bank and shared object hazards
560575
if (!MemoryAccessCycles.empty()) {
561-
FuncUnitWrapper MemoryAccessCycle(/*SlotSet=*/0, MemoryBanks,
576+
const SlotBits Slots = 0;
577+
const SlotBits Conflicts = 0;
578+
FuncUnitWrapper MemoryAccessCycle(Slots, Conflicts, MemoryBanks,
562579
MemObjectsBits);
563580

564581
for (auto Cycles : MemoryAccessCycles) {
@@ -644,13 +661,16 @@ void AIEHazardRecognizer::enterResources(
644661
std::optional<int> FUDepthLimit) {
645662

646663
// Append slot usage
647-
FuncUnitWrapper EmissionCycle(SlotSet);
664+
const SlotBits Conflicts = 0;
665+
FuncUnitWrapper EmissionCycle(SlotSet, Conflicts);
648666
Scoreboard[DeltaCycles] |= EmissionCycle;
649667

650668
// Append memory bank usage
651669
if (!MemoryAccessCycles.empty()) {
670+
const SlotBits Slots = 0;
671+
const SlotBits Conflicts = 0;
652672
FuncUnitWrapper MemoryBankAndObjectsAccessCycle(
653-
/*SlotSet=*/0, MemoryBanks, MemObjectsBits);
673+
Slots, Conflicts, MemoryBanks, MemObjectsBits);
654674
for (auto Cycles : MemoryAccessCycles) {
655675
assert(Scoreboard.isInRange(DeltaCycles + Cycles - 1));
656676
Scoreboard[DeltaCycles + Cycles - 1] |= MemoryBankAndObjectsAccessCycle;

llvm/lib/Target/AIE/AIEHazardRecognizer.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class FuncUnitWrapper {
7272

7373
/// The occupied slots. This is currently redundant with Bundle
7474
SlotBits Slots = 0;
75+
/// Conflicts are just for speeding up conflict detection. They may be present
76+
/// in cycles to be merged, but they will not be merged into the scoreboard.
77+
SlotBits Conflicts = 0;
7578

7679
/// The occupied bank
7780
MemoryBankBits MemoryBanks = 0;
@@ -105,10 +108,11 @@ class FuncUnitWrapper {
105108
/// Make this conflict with any non-empty cycle
106109
void blockResources();
107110
FuncUnitWrapper() = default;
108-
FuncUnitWrapper(SlotBits Slots, MemoryBankBits MemoryBanks = 0,
111+
FuncUnitWrapper(SlotBits Slots, SlotBits Conflicts,
112+
MemoryBankBits MemoryBanks = 0,
109113
MemoryObjectsBits MemObjectsBits = 0)
110-
: Slots(Slots), MemoryBanks(MemoryBanks), MemObjectsBits(MemObjectsBits) {
111-
}
114+
: Slots(Slots), Conflicts(Conflicts), MemoryBanks(MemoryBanks),
115+
MemObjectsBits(MemObjectsBits) {}
112116

113117
/// Compare two FuncUnitWrappers for equality. This is only used for
114118
/// dumping purposes, quite literally saying "this looks the same"
@@ -268,9 +272,10 @@ class AIEHazardRecognizer : public ScheduleHazardRecognizer {
268272
static bool
269273
checkConflict(const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
270274
const InstrItineraryData *ItinData, unsigned SchedClass,
271-
SlotBits SlotSet, MemoryBankBits MemoryBanks,
272-
uint64_t MemObjectsBits, SmallVector<int, 2> MemoryAccessCycles,
273-
int DeltaCycles, std::optional<int> FUDepthLimit);
275+
SlotBits SlotSet, SlotBits Conflicts,
276+
MemoryBankBits MemoryBanks, uint64_t MemObjectsBits,
277+
SmallVector<int, 2> MemoryAccessCycles, int DeltaCycles,
278+
std::optional<int> FUDepthLimit);
274279

275280
static void enterResources(ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
276281
const InstrItineraryData *ItinData,
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
3+
# See https://llvm.org/LICENSE.txt for license information.
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
# (c) Copyright 2025 Advanced Micro Devices, Inc. or its affiliates
7+
8+
# RUN: llc -mtriple=aie2 -run-pass=postmisched %s -o - | FileCheck %s
9+
10+
11+
# This is an explicit test for the optimized slot conflict detection
12+
# On aie2, this centers around XM conflicting with X and M, but not with others.
13+
# also, X should not conflict with M
14+
# The code below generates two bundles, one lda + lng, one alu + mv
15+
16+
---
17+
name: f
18+
alignment: 16
19+
tracksRegLiveness: true
20+
body: |
21+
; CHECK-LABEL: name: f
22+
; CHECK: bb.0:
23+
; CHECK-NEXT: successors: %bb.1(0x80000000)
24+
; CHECK-NEXT: liveins: $r0
25+
; CHECK-NEXT: {{ $}}
26+
; CHECK-NEXT: $r1 = MOVX_alu_cg 1
27+
; CHECK-NEXT: $r2 = MOVXM_lng_cg 12345
28+
; CHECK-NEXT: {{ $}}
29+
; CHECK-NEXT: bb.1:
30+
; CHECK-NEXT: successors: %bb.2(0x80000000)
31+
; CHECK-NEXT: liveins: $r0
32+
; CHECK-NEXT: {{ $}}
33+
; CHECK-NEXT: $r1 = MOV_mv_scl $r0
34+
; CHECK-NEXT: $r2 = MOVXM_lng_cg 12345
35+
; CHECK-NEXT: {{ $}}
36+
; CHECK-NEXT: bb.2:
37+
; CHECK-NEXT: successors: %bb.3(0x80000000)
38+
; CHECK-NEXT: liveins: $r0
39+
; CHECK-NEXT: {{ $}}
40+
; CHECK-NEXT: BUNDLE implicit-def $r1, implicit-def $r2 {
41+
; CHECK-NEXT: $r1 = MOVA_lda_cg 2
42+
; CHECK-NEXT: $r2 = MOVXM_lng_cg 12345
43+
; CHECK-NEXT: }
44+
; CHECK-NEXT: {{ $}}
45+
; CHECK-NEXT: bb.3:
46+
; CHECK-NEXT: liveins: $r0
47+
; CHECK-NEXT: {{ $}}
48+
; CHECK-NEXT: RET implicit $lr
49+
; CHECK-NEXT: NOP
50+
; CHECK-NEXT: NOP
51+
; CHECK-NEXT: NOP
52+
; CHECK-NEXT: NOP
53+
; CHECK-NEXT: BUNDLE implicit-def $r1, implicit-def $r2, implicit killed $r0 {
54+
; CHECK-NEXT: $r1 = MOVX_alu_cg 1
55+
; CHECK-NEXT: $r2 = MOV_mv_scl killed $r0
56+
; CHECK-NEXT: }
57+
; CHECK-NEXT: DelayedSchedBarrier implicit killed $r1, implicit killed $r2
58+
bb.0:
59+
liveins: $r0
60+
$r1 = MOVX_alu_cg 1
61+
$r2 = MOVXM_lng_cg 12345
62+
63+
bb.1:
64+
liveins: $r0
65+
$r1 = MOV_mv_scl $r0
66+
$r2 = MOVXM_lng_cg 12345
67+
68+
bb.2:
69+
liveins: $r0
70+
$r1 = MOVA_lda_cg 2
71+
$r2 = MOVXM_lng_cg 12345
72+
73+
bb.3:
74+
liveins: $r0
75+
$r1 = MOVX_alu_cg 1
76+
$r2 = MOV_mv_scl $r0
77+
78+
RET implicit $lr
79+
DelayedSchedBarrier implicit $r1, implicit $r2
80+
...

llvm/unittests/Target/AIE/HazardRecognizerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class MockHR : public AIEHazardRecognizer {
135135
bool hazard(unsigned SchedClass, int Delta, SlotBits SlotSet = 0,
136136
MemoryBankBits MemoryBanks = 0, MemoryObjectsBits ObjectsBits = 0,
137137
SmallVector<int, 2> MemoryAccessCycles = {}) {
138-
return checkConflict(MockScoreboard, &Itins, SchedClass, SlotSet,
138+
return checkConflict(MockScoreboard, &Itins, SchedClass, SlotSet, SlotSet,
139139
MemoryBanks, ObjectsBits, MemoryAccessCycles, Delta,
140140
std::nullopt);
141141
}

0 commit comments

Comments
 (0)