Skip to content

Commit da5d62a

Browse files
author
pchaudhuri-nv
committed
fic
1 parent 0fc05aa commit da5d62a

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// The test incorrect classifies the pattern as a typecast and it ends up with
2+
// typecast DAG with zero arguements, leading to llvm-tblgen crash.
3+
// This test will check if the error is gracefully handled without any crash.
4+
5+
// RUN: not llvm-tblgen -gen-dag-isel -I %p/../../include %s 2>&1 | FileCheck -check-prefix=ERROR-CHK %s
6+
7+
include "llvm/Target/Target.td"
8+
9+
class MyReg<string n>
10+
: Register<n> {
11+
let Namespace = "MyTarget";
12+
}
13+
14+
def X0 : MyReg<"x0">;
15+
def X1 : MyReg<"x1">;
16+
17+
def XRegs : RegisterClass<"MyTarget", [i64], 64, (add X0, X1)>;
18+
19+
class TestInstruction : Instruction {
20+
let Size = 2;
21+
let Namespace = "MyTarget";
22+
let hasSideEffects = false;
23+
let hasExtraSrcRegAllocReq = false;
24+
let hasExtraDefRegAllocReq = false;
25+
26+
field bits<16> Inst;
27+
bits<3> dst;
28+
bits<3> src;
29+
bits<3> opcode;
30+
31+
let Inst{2-0} = dst;
32+
let Inst{5-3} = src;
33+
let Inst{7-5} = opcode;
34+
}
35+
36+
def MY_LOAD : TestInstruction {
37+
let OutOperandList = (outs XRegs:$dst);
38+
let InOperandList = (ins ptr_rc:$ptr);
39+
let AsmString = "my_load $dst, $ptr";
40+
let opcode = 0;
41+
}
42+
43+
// ERROR-CHK: [[@LINE+1]]:1: error: {{.*}} type cast should not have zero arguments!
44+
def : Pat<
45+
(i64 (load (iPTR:$src))),
46+
(MY_LOAD $val, $src)
47+
>;
48+
49+
def MyTargetISA : InstrInfo;
50+
def MyTarget : Target { let InstructionSet = MyTargetISA; }

llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,7 +3010,13 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
30103010
return nullptr;
30113011
}
30123012

3013-
auto ParseCastOperand = [this](const DagInit *Dag, StringRef OpName) {
3013+
auto ParseCastOperand = [this](const DagInit *Dag,
3014+
StringRef OpName) -> TreePatternNodePtr {
3015+
if (Dag->getNumArgs() == 0) {
3016+
error("type cast should not have zero arguments!");
3017+
return nullptr;
3018+
}
3019+
30143020
if (Dag->getNumArgs() != 1)
30153021
error("Type cast only takes one operand!");
30163022

@@ -3024,6 +3030,8 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
30243030
// If the operator is a list (of value types), then this must be "type cast"
30253031
// of a leaf node with multiple results.
30263032
TreePatternNodePtr New = ParseCastOperand(Dag, OpName);
3033+
if (!New)
3034+
return nullptr;
30273035

30283036
size_t NumTypes = New->getNumTypes();
30293037
if (LI->empty() || LI->size() != NumTypes)
@@ -3049,6 +3057,8 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
30493057
// If the operator is a ValueType, then this must be "type cast" of a leaf
30503058
// node.
30513059
TreePatternNodePtr New = ParseCastOperand(Dag, OpName);
3060+
if (!New)
3061+
return nullptr;
30523062

30533063
if (New->getNumTypes() != 1)
30543064
error("ValueType cast can only have one type!");
@@ -3606,10 +3616,15 @@ void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
36063616
// If this is not a set, verify that the children nodes are not void typed,
36073617
// and recurse.
36083618
for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
3609-
if (Pat->getChild(i).getNumTypes() == 0)
3619+
TreePatternNodePtr Child = Pat->getChildShared(i);
3620+
if (!Child) {
3621+
I.error("Child node at index " + Twine(i) + " is null!");
3622+
continue;
3623+
}
3624+
if (Child->getNumTypes() == 0)
36103625
I.error("Cannot have void nodes inside of patterns!");
3611-
FindPatternInputsAndOutputs(I, Pat->getChildShared(i), InstInputs,
3612-
InstResults, InstImpResults);
3626+
FindPatternInputsAndOutputs(I, Child, InstInputs, InstResults,
3627+
InstImpResults);
36133628
}
36143629

36153630
// If this is a non-leaf node with no children, treat it basically as if

0 commit comments

Comments
 (0)