Skip to content

Commit 6d4a3e9

Browse files
committed
Revert "[TableGen] Use heap allocated arrays instead of vectors for TreePatternNode::Types and ResultPerm. NFC"
While working on DAGISelMatcherEmitter I've hit several runtime errors caused by accessing TreePatternNode::Types out of bounds. These were difficult to debug because the switch from std::vector to unique_ptr removes bounds checking. I don't think the slight reduction in class size is worth the extra debugging and memory safety problems, so I suggest we revert this. This reverts commit d34125a. Differential Revision: https://reviews.llvm.org/D154781
1 parent 4c66b03 commit 6d4a3e9

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

llvm/utils/TableGen/CodeGenDAGPatterns.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,7 @@ bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo,
17621762
}
17631763

17641764
bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
1765-
for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
1765+
for (unsigned i = 0, e = Types.size(); i != e; ++i)
17661766
if (!TP.getInfer().isConcrete(Types[i], true))
17671767
return true;
17681768
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
@@ -1772,7 +1772,7 @@ bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
17721772
}
17731773

17741774
bool TreePatternNode::hasProperTypeByHwMode() const {
1775-
for (const TypeSetByHwMode &S : getExtTypes())
1775+
for (const TypeSetByHwMode &S : Types)
17761776
if (!S.isSimple())
17771777
return true;
17781778
for (const TreePatternNodePtr &C : Children)
@@ -1782,7 +1782,7 @@ bool TreePatternNode::hasProperTypeByHwMode() const {
17821782
}
17831783

17841784
bool TreePatternNode::hasPossibleType() const {
1785-
for (const TypeSetByHwMode &S : getExtTypes())
1785+
for (const TypeSetByHwMode &S : Types)
17861786
if (!S.isPossible())
17871787
return false;
17881788
for (const TreePatternNodePtr &C : Children)
@@ -1792,7 +1792,7 @@ bool TreePatternNode::hasPossibleType() const {
17921792
}
17931793

17941794
bool TreePatternNode::setDefaultMode(unsigned Mode) {
1795-
for (TypeSetByHwMode &S : getExtTypes()) {
1795+
for (TypeSetByHwMode &S : Types) {
17961796
S.makeSimple(Mode);
17971797
// Check if the selected mode had a type conflict.
17981798
if (S.get(DefaultMode).empty())
@@ -1932,7 +1932,7 @@ void TreePatternNode::print(raw_ostream &OS) const {
19321932
else
19331933
OS << '(' << getOperator()->getName();
19341934

1935-
for (unsigned i = 0, e = getNumTypes(); i != e; ++i) {
1935+
for (unsigned i = 0, e = Types.size(); i != e; ++i) {
19361936
OS << ':';
19371937
getExtType(i).writeToStream(OS);
19381938
}
@@ -2024,7 +2024,7 @@ TreePatternNodePtr TreePatternNode::clone() const {
20242024
}
20252025
New->setName(getName());
20262026
New->setNamesAsPredicateArg(getNamesAsPredicateArg());
2027-
llvm::copy(getExtTypes(), New->getExtTypes().begin());
2027+
New->Types = Types;
20282028
New->setPredicateCalls(getPredicateCalls());
20292029
New->setGISelFlagsRecord(getGISelFlagsRecord());
20302030
New->setTransformFn(getTransformFn());
@@ -2034,7 +2034,7 @@ TreePatternNodePtr TreePatternNode::clone() const {
20342034
/// RemoveAllTypes - Recursively strip all the types of this tree.
20352035
void TreePatternNode::RemoveAllTypes() {
20362036
// Reset to unknown type.
2037-
std::fill(getExtTypes().begin(), getExtTypes().end(), TypeSetByHwMode());
2037+
std::fill(Types.begin(), Types.end(), TypeSetByHwMode());
20382038
if (isLeaf()) return;
20392039
for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
20402040
getChild(i)->RemoveAllTypes();
@@ -2130,10 +2130,10 @@ void TreePatternNode::InlinePatternFragments(
21302130
R->setPredicateCalls(getPredicateCalls());
21312131
R->setGISelFlagsRecord(getGISelFlagsRecord());
21322132
R->setTransformFn(getTransformFn());
2133-
for (unsigned i = 0, e = getNumTypes(); i != e; ++i) {
2133+
for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
21342134
R->setType(i, getExtType(i));
2135+
for (unsigned i = 0, e = getNumResults(); i != e; ++i)
21352136
R->setResultIndex(i, getResultIndex(i));
2136-
}
21372137

21382138
// Register alternative.
21392139
OutAlternatives.push_back(R);
@@ -2468,15 +2468,15 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
24682468
if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
24692469
// If it's a regclass or something else known, include the type.
24702470
bool MadeChange = false;
2471-
for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
2471+
for (unsigned i = 0, e = Types.size(); i != e; ++i)
24722472
MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i,
24732473
NotRegisters,
24742474
!hasName(), TP), TP);
24752475
return MadeChange;
24762476
}
24772477

24782478
if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
2479-
assert(getNumTypes() == 1 && "Invalid IntInit");
2479+
assert(Types.size() == 1 && "Invalid IntInit");
24802480

24812481
// Int inits are always integers. :)
24822482
bool MadeChange = TP.getInfer().EnforceInteger(Types[0]);
@@ -2713,7 +2713,7 @@ bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
27132713
bool MadeChange = false;
27142714

27152715
if (!NotRegisters) {
2716-
assert(getNumTypes() == 1 && "ComplexPatterns only produce one result!");
2716+
assert(Types.size() == 1 && "ComplexPatterns only produce one result!");
27172717
Record *T = CDP.getComplexPattern(getOperator()).getValueType();
27182718
const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
27192719
const ValueTypeByHwMode VVT = getValueTypeByHwMode(T, CGH);

llvm/utils/TableGen/CodeGenDAGPatterns.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -626,16 +626,13 @@ struct TreePredicateCall {
626626
};
627627

628628
class TreePatternNode : public RefCountedBase<TreePatternNode> {
629-
/// Number of results for this node.
630-
unsigned NumResults;
631-
632629
/// The type of each node result. Before and during type inference, each
633630
/// result may be a set of possible types. After (successful) type inference,
634631
/// each is a single concrete type.
635-
std::unique_ptr<TypeSetByHwMode[]> Types;
632+
std::vector<TypeSetByHwMode> Types;
636633

637634
/// The index of each result in results of the pattern.
638-
std::unique_ptr<unsigned[]> ResultPerm;
635+
std::vector<unsigned> ResultPerm;
639636

640637
/// OperatorOrVal - The Record for the operator if this is an interior node
641638
/// (not a leaf) or the init value (e.g. the "GPRC" record, or "7") for a
@@ -654,7 +651,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
654651

655652
/// TransformFn - The transformation function to execute on this node before
656653
/// it can be substituted into the resulting instruction on a pattern match.
657-
Record *TransformFn = nullptr;
654+
Record *TransformFn;
658655

659656
std::vector<TreePatternNodePtr> Children;
660657

@@ -664,16 +661,17 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
664661

665662
public:
666663
TreePatternNode(Record *Op, std::vector<TreePatternNodePtr> Ch,
667-
unsigned numResults)
668-
: NumResults(numResults), Types(new TypeSetByHwMode[numResults]),
669-
ResultPerm(new unsigned[numResults]), OperatorOrVal(Op),
670-
Children(std::move(Ch)) {
671-
std::iota(ResultPerm.get(), ResultPerm.get() + numResults, 0);
664+
unsigned NumResults)
665+
: OperatorOrVal(Op), TransformFn(nullptr), Children(std::move(Ch)) {
666+
Types.resize(NumResults);
667+
ResultPerm.resize(NumResults);
668+
std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
672669
}
673-
TreePatternNode(Init *val, unsigned numResults) // leaf ctor
674-
: NumResults(numResults), Types(new TypeSetByHwMode[numResults]),
675-
ResultPerm(new unsigned[numResults]), OperatorOrVal(val) {
676-
std::iota(ResultPerm.get(), ResultPerm.get() + numResults, 0);
670+
TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
671+
: OperatorOrVal(val), TransformFn(nullptr) {
672+
Types.resize(NumResults);
673+
ResultPerm.resize(NumResults);
674+
std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
677675
}
678676

679677
bool hasName() const { return !Name.empty(); }
@@ -693,16 +691,11 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
693691
bool isLeaf() const { return isa<Init *>(OperatorOrVal); }
694692

695693
// Type accessors.
696-
unsigned getNumTypes() const { return NumResults; }
694+
unsigned getNumTypes() const { return Types.size(); }
697695
ValueTypeByHwMode getType(unsigned ResNo) const {
698696
return Types[ResNo].getValueTypeByHwMode();
699697
}
700-
ArrayRef<TypeSetByHwMode> getExtTypes() const {
701-
return ArrayRef(Types.get(), NumResults);
702-
}
703-
MutableArrayRef<TypeSetByHwMode> getExtTypes() {
704-
return MutableArrayRef(Types.get(), NumResults);
705-
}
698+
const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
706699
const TypeSetByHwMode &getExtType(unsigned ResNo) const {
707700
return Types[ResNo];
708701
}
@@ -719,6 +712,7 @@ class TreePatternNode : public RefCountedBase<TreePatternNode> {
719712
return Types[ResNo].empty();
720713
}
721714

715+
unsigned getNumResults() const { return ResultPerm.size(); }
722716
unsigned getResultIndex(unsigned ResNo) const { return ResultPerm[ResNo]; }
723717
void setResultIndex(unsigned ResNo, unsigned RI) { ResultPerm[ResNo] = RI; }
724718

llvm/utils/TableGen/DAGISelMatcherGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ void MatcherGen::EmitResultCode() {
10671067
SmallVector<unsigned, 8> Results(Ops);
10681068

10691069
// Apply result permutation.
1070-
for (unsigned ResNo = 0; ResNo < Pattern.getDstPattern()->getNumTypes();
1070+
for (unsigned ResNo = 0; ResNo < Pattern.getDstPattern()->getNumResults();
10711071
++ResNo) {
10721072
Results[ResNo] = Ops[Pattern.getDstPattern()->getResultIndex(ResNo)];
10731073
}

0 commit comments

Comments
 (0)