Skip to content

Commit 0bbb60d

Browse files
committed
Make conflicting properties a hard error instead of preferring the less strict one
1 parent d9c9e48 commit 0bbb60d

File tree

4 files changed

+96
-61
lines changed

4 files changed

+96
-61
lines changed

llvm/include/llvm/CodeGen/MIRYamlMapping.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,9 @@ struct MachineFunction {
731731
bool HasWinCFI = false;
732732

733733
// Computed properties that should be overridable
734-
bool NoPHIs = false;
735-
bool IsSSA = false;
736-
bool NoVRegs = false;
734+
std::optional<bool> NoPHIs;
735+
std::optional<bool> IsSSA;
736+
std::optional<bool> NoVRegs;
737737

738738
bool CallsEHReturn = false;
739739
bool CallsUnwindInit = false;
@@ -777,9 +777,9 @@ template <> struct MappingTraits<MachineFunction> {
777777

778778
// PHIs must be not be capitalized, since it will clash with the MIR opcode
779779
// leading to false-positive FileCheck hits with CHECK-NOT
780-
YamlIO.mapOptional("noPhis", MF.NoPHIs, true);
781-
YamlIO.mapOptional("isSSA", MF.IsSSA, true);
782-
YamlIO.mapOptional("noVRegs", MF.NoVRegs, true);
780+
YamlIO.mapOptional("noPhis", MF.NoPHIs, std::optional<bool>());
781+
YamlIO.mapOptional("isSSA", MF.IsSSA, std::optional<bool>());
782+
YamlIO.mapOptional("noVRegs", MF.NoVRegs, std::optional<bool>());
783783

784784
YamlIO.mapOptional("callsEHReturn", MF.CallsEHReturn, false);
785785
YamlIO.mapOptional("callsUnwindInit", MF.CallsUnwindInit, false);

llvm/lib/CodeGen/MIRParser/MIRParser.cpp

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class MIRParserImpl {
178178
SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
179179
SMRange SourceRange);
180180

181-
void computeFunctionProperties(MachineFunction &MF,
181+
bool computeFunctionProperties(MachineFunction &MF,
182182
const yaml::MachineFunction &YamlMF);
183183

184184
void setupDebugValueTracking(MachineFunction &MF,
@@ -374,7 +374,7 @@ static bool isSSA(const MachineFunction &MF) {
374374
return true;
375375
}
376376

377-
void MIRParserImpl::computeFunctionProperties(
377+
bool MIRParserImpl::computeFunctionProperties(
378378
MachineFunction &MF, const yaml::MachineFunction &YamlMF) {
379379
MachineFunctionProperties &Properties = MF.getProperties();
380380

@@ -401,25 +401,60 @@ void MIRParserImpl::computeFunctionProperties(
401401
}
402402
}
403403

404-
// Don't overwrite NoPHIs if the input MIR explicitly set it to false
405-
if (YamlMF.NoPHIs && !HasPHI)
406-
Properties.set(MachineFunctionProperties::Property::NoPHIs);
404+
// Helper function to sanity-check and set properties that are computed, but
405+
// may be explicitly set from the input MIR
406+
auto ComputedPropertyHelper =
407+
[&Properties](std::optional<bool> ExplicitProp, bool ComputedProp,
408+
MachineFunctionProperties::Property P) -> bool {
409+
// Prefer whatever is set explicitly by the input MIR
410+
if (ExplicitProp.has_value()) {
411+
if (*ExplicitProp) {
412+
// Check for conflicts with the computed value
413+
if (!ComputedProp)
414+
return true;
415+
416+
Properties.set(P);
417+
} else
418+
Properties.reset(P);
419+
420+
return false;
421+
}
422+
423+
// No explicit value given, so use computed value
424+
if (ComputedProp)
425+
Properties.set(P);
426+
else
427+
Properties.reset(P);
428+
429+
return false;
430+
};
431+
432+
if (ComputedPropertyHelper(YamlMF.NoPHIs, !HasPHI,
433+
MachineFunctionProperties::Property::NoPHIs)) {
434+
return error(MF.getName() +
435+
" has explicit property NoPhi, but contains at least one PHI");
436+
}
407437

408438
MF.setHasInlineAsm(HasInlineAsm);
409439

410440
if (HasTiedOps && AllTiedOpsRewritten)
411441
Properties.set(MachineFunctionProperties::Property::TiedOpsRewritten);
412442

413-
// Don't overwrite IsSSA if the input MIR explicitly set it to false
414-
if (YamlMF.IsSSA && isSSA(MF))
415-
Properties.set(MachineFunctionProperties::Property::IsSSA);
416-
else
417-
Properties.reset(MachineFunctionProperties::Property::IsSSA);
443+
if (ComputedPropertyHelper(YamlMF.IsSSA, isSSA(MF),
444+
MachineFunctionProperties::Property::IsSSA)) {
445+
return error(MF.getName() +
446+
" has explicit property IsSSA, but is not valid SSA");
447+
}
418448

419-
// Don't overwrite NoVRegs if the input MIR explicitly set it to false
420449
const MachineRegisterInfo &MRI = MF.getRegInfo();
421-
if (YamlMF.NoVRegs && MRI.getNumVirtRegs() == 0)
422-
Properties.set(MachineFunctionProperties::Property::NoVRegs);
450+
if (ComputedPropertyHelper(YamlMF.NoVRegs, MRI.getNumVirtRegs() == 0,
451+
MachineFunctionProperties::Property::NoVRegs)) {
452+
return error(
453+
MF.getName() +
454+
" has explicit property NoVRegs, but contains virtual registers");
455+
}
456+
457+
return false;
423458
}
424459

425460
bool MIRParserImpl::initializeCallSiteInfo(
@@ -602,7 +637,8 @@ MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
602637
MachineRegisterInfo &MRI = MF.getRegInfo();
603638
MRI.freezeReservedRegs();
604639

605-
computeFunctionProperties(MF, YamlMF);
640+
if (computeFunctionProperties(MF, YamlMF))
641+
return false;
606642

607643
if (initializeCallSiteInfo(PFS, YamlMF))
608644
return false;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# RUN: not llc -run-pass none -o /dev/null %s 2>&1 | FileCheck %s
2+
3+
# Test that computed properties are not conflicting with explicitly set
4+
# properties
5+
6+
---
7+
# CHECK-LABEL: TestNoPhisOverrideConflict
8+
# CHECK-SAME: has explicit property NoPhi, but contains at least one PHI
9+
name: TestNoPhisOverrideConflict
10+
noPhis: true
11+
tracksRegLiveness: true
12+
body: |
13+
bb.0:
14+
%0:_(s32) = IMPLICIT_DEF
15+
16+
bb.1:
17+
%1:_(s32) = PHI %0, %bb.0, %1, %bb.1
18+
G_BR %bb.1
19+
...
20+
---
21+
# CHECK-LABEL: TestIsSSAOverrideConflict
22+
# CHECK-SAME: has explicit property IsSSA, but is not valid SSA
23+
name: TestIsSSAOverrideConflict
24+
isSSA: true
25+
body: |
26+
bb.0:
27+
%0:_(s32) = IMPLICIT_DEF
28+
%0:_(s32) = IMPLICIT_DEF
29+
...
30+
---
31+
# CHECK-LABEL: TestNoVRegsOverrideConflict
32+
# CHECK-SAME: has explicit property NoVRegs, but contains virtual registers
33+
name: TestNoVRegsOverrideConflict
34+
noVRegs: true
35+
body: |
36+
bb.0:
37+
%0:_(s32) = IMPLICIT_DEF
38+
...
Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# RUN: llc -run-pass none -o - %s | FileCheck %s
2-
# Test that we can disable certain properties that are normally computed. This
3-
# override is a soft-override (only allows overriding when it relaxes the
4-
# property). Therefore, a conflicting override (e.g. setting noPhis to true in
5-
# the presence of PHI nodes) should not result in an error, but instead should
6-
# use the computed property instead (noPhis = false in the mentioned example).
2+
3+
# Test that we can disable certain properties that are normally computed
74

85
---
96
# CHECK-LABEL: name: TestNoPhis
@@ -26,21 +23,6 @@ name: TestNoPhisOverrideTrue
2623
noPhis: true
2724
...
2825
---
29-
# CHECK-LABEL: name: TestNoPhisOverrideConflict
30-
# CHECK: noPhis: false
31-
# CHECK: ...
32-
name: TestNoPhisOverrideConflict
33-
noPhis: true
34-
tracksRegLiveness: true
35-
body: |
36-
bb.0:
37-
%0:_(s32) = IMPLICIT_DEF
38-
39-
bb.1:
40-
%1:_(s32) = PHI %0, %bb.0, %0, %bb.1
41-
G_BR %bb.1
42-
...
43-
---
4426
# CHECK-LABEL: name: TestIsSSA
4527
# CHECK: isSSA: true
4628
# CHECK: ...
@@ -61,17 +43,6 @@ name: TestIsSSAOverrideTrue
6143
isSSA: true
6244
...
6345
---
64-
# CHECK-LABEL: name: TestIsSSAOverrideConflict
65-
# CHECK: isSSA: false
66-
# CHECK: ...
67-
name: TestIsSSAOverrideConflict
68-
isSSA: true
69-
body: |
70-
bb.0:
71-
%0:_(s32) = IMPLICIT_DEF
72-
%0:_(s32) = IMPLICIT_DEF
73-
...
74-
---
7546
# CHECK-LABEL: name: TestNoVRegs
7647
# CHECK: noVRegs: true
7748
# CHECK: ...
@@ -91,13 +62,3 @@ noVRegs: false
9162
name: TestNoVRegsOverrideTrue
9263
noVRegs: true
9364
...
94-
---
95-
# CHECK-LABEL: name: TestNoVRegsOverrideConflict
96-
# CHECK: noVRegs: false
97-
# CHECK: ...
98-
name: TestNoVRegsOverrideConflict
99-
noVRegs: true
100-
body: |
101-
bb.0:
102-
%0:_(s32) = IMPLICIT_DEF
103-
...

0 commit comments

Comments
 (0)