Skip to content

Commit 377b0e2

Browse files
committed
[TableGen] Don't elide bitconverts in PatFrag fragments.
Summary: In the DAG pattern backend, `SimplifyTree` simplifies a pattern by removing bitconverts between two identical types. But that function is also run on the fragments list in instances of `PatFrags`, in which the types haven't been specified yet. So the input and output of the bitconvert always evaluate to the empty set of types, which makes them compare equal. So the test always passes, and bitconverts are unconditionally removed from the PatFrag RHS. Fixed by spotting the empty type set and using it to inhibit the optimization. Reviewers: nhaehnle, hfinkel Reviewed By: nhaehnle Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D74627
1 parent 516ba15 commit 377b0e2

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: llvm-tblgen -gen-dag-isel -I %p/../../include %s 2>&1 | FileCheck %s
2+
3+
include "llvm/Target/Target.td"
4+
5+
// Minimal Target definition
6+
def DemoInstrInfo : InstrInfo;
7+
def Demo : Target {
8+
let InstructionSet = DemoInstrInfo;
9+
}
10+
11+
// Some registers which can hold ints or floats
12+
foreach i = 0-7 in
13+
def "R" # i: Register<"r" # i>;
14+
def GPR : RegisterClass<"Demo", [i32, f32], 32, (sequence "R%u", 0, 7)>;
15+
16+
// Instruction to convert an int to a float
17+
def i2f : Instruction {
18+
let Size = 2;
19+
let OutOperandList = (outs GPR:$dst);
20+
let InOperandList = (ins GPR:$src);
21+
let AsmString = "i2f $dst, $src";
22+
}
23+
24+
// Some kind of special type-conversion node supported by this target
25+
def specialconvert : SDNode<"TEST_TARGET_ISD::SPECIAL_CONVERT", SDTUnaryOp>;
26+
27+
// A PatFrags that matches either bitconvert or the special version
28+
def anyconvert : PatFrags<(ops node:$src),
29+
[(bitconvert node:$src),
30+
(specialconvert node:$src)]>;
31+
32+
// And a rule that matches that PatFrag and turns it into i2f
33+
def : Pat<(f32 (anyconvert (i32 GPR:$val))), (i2f GPR:$val)>;
34+
35+
// CHECK: SwitchOpcode{{.*}}ISD::BITCAST
36+
// CHECK: MorphNodeTo1{{.*}}i2f
37+
// CHECK: SwitchOpcode{{.*}}TEST_TARGET_ISD::SPECIAL_CONVERT
38+
// CHECK: MorphNodeTo1{{.*}}i2f

llvm/utils/TableGen/CodeGenDAGPatterns.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,8 +2918,15 @@ static bool SimplifyTree(TreePatternNodePtr &N) {
29182918

29192919
// If we have a bitconvert with a resolved type and if the source and
29202920
// destination types are the same, then the bitconvert is useless, remove it.
2921+
//
2922+
// We make an exception if the types are completely empty. This can come up
2923+
// when the pattern being simplified is in the Fragments list of a PatFrags,
2924+
// so that the operand is just an untyped "node". In that situation we leave
2925+
// bitconverts unsimplified, and simplify them later once the fragment is
2926+
// expanded into its true context.
29212927
if (N->getOperator()->getName() == "bitconvert" &&
29222928
N->getExtType(0).isValueTypeByHwMode(false) &&
2929+
!N->getExtType(0).empty() &&
29232930
N->getExtType(0) == N->getChild(0)->getExtType(0) &&
29242931
N->getName().empty()) {
29252932
N = N->getChildShared(0);

0 commit comments

Comments
 (0)