1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
//===- PatternParser.cpp ----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Common/GlobalISel/PatternParser.h"
#include "Basic/CodeGenIntrinsics.h"
#include "Common/CodeGenTarget.h"
#include "Common/GlobalISel/CombinerUtils.h"
#include "Common/GlobalISel/Patterns.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
namespace llvm {
namespace gi {
static constexpr StringLiteral MIFlagsEnumClassName = "MIFlagEnum";
namespace {
class PrettyStackTraceParse : public PrettyStackTraceEntry {
const Record &Def;
public:
PrettyStackTraceParse(const Record &Def) : Def(Def) {}
void print(raw_ostream &OS) const override {
if (Def.isSubClassOf("GICombineRule"))
OS << "Parsing GICombineRule '" << Def.getName() << '\'';
else if (Def.isSubClassOf(PatFrag::ClassName))
OS << "Parsing " << PatFrag::ClassName << " '" << Def.getName() << '\'';
else
OS << "Parsing '" << Def.getName() << '\'';
OS << '\n';
}
};
} // namespace
bool PatternParser::parsePatternList(
const DagInit &List,
function_ref<bool(std::unique_ptr<Pattern>)> ParseAction,
StringRef Operator, StringRef AnonPatNamePrefix) {
if (List.getOperatorAsDef(DiagLoc)->getName() != Operator) {
PrintError(DiagLoc, "Expected " + Operator + " operator");
return false;
}
if (List.getNumArgs() == 0) {
PrintError(DiagLoc, Operator + " pattern list is empty");
return false;
}
// The match section consists of a list of matchers and predicates. Parse each
// one and add the equivalent GIMatchDag nodes, predicates, and edges.
for (unsigned I = 0; I < List.getNumArgs(); ++I) {
Init *Arg = List.getArg(I);
std::string Name = List.getArgName(I)
? List.getArgName(I)->getValue().str()
: ("__" + AnonPatNamePrefix + "_" + Twine(I)).str();
if (auto Pat = parseInstructionPattern(*Arg, Name)) {
if (!ParseAction(std::move(Pat)))
return false;
continue;
}
if (auto Pat = parseWipMatchOpcodeMatcher(*Arg, Name)) {
if (!ParseAction(std::move(Pat)))
return false;
continue;
}
// Parse arbitrary C++ code
if (const auto *StringI = dyn_cast<StringInit>(Arg)) {
auto CXXPat = std::make_unique<CXXPattern>(*StringI, insertStrRef(Name));
if (!ParseAction(std::move(CXXPat)))
return false;
continue;
}
PrintError(DiagLoc,
"Failed to parse pattern: '" + Arg->getAsString() + '\'');
return false;
}
return true;
}
static const CodeGenInstruction &
getInstrForIntrinsic(const CodeGenTarget &CGT, const CodeGenIntrinsic *I) {
StringRef Opc;
if (I->isConvergent) {
Opc = I->hasSideEffects ? "G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS"
: "G_INTRINSIC_CONVERGENT";
} else {
Opc = I->hasSideEffects ? "G_INTRINSIC_W_SIDE_EFFECTS" : "G_INTRINSIC";
}
RecordKeeper &RK = I->TheDef->getRecords();
return CGT.getInstruction(RK.getDef(Opc));
}
static const CodeGenIntrinsic *getCodeGenIntrinsic(Record *R) {
// Intrinsics need to have a static lifetime because the match table keeps
// references to CodeGenIntrinsic objects.
static DenseMap<const Record *, std::unique_ptr<CodeGenIntrinsic>>
AllIntrinsics;
auto &Ptr = AllIntrinsics[R];
if (!Ptr)
Ptr = std::make_unique<CodeGenIntrinsic>(R, std::vector<Record *>());
return Ptr.get();
}
std::unique_ptr<Pattern>
PatternParser::parseInstructionPattern(const Init &Arg, StringRef Name) {
const DagInit *DagPat = dyn_cast<DagInit>(&Arg);
if (!DagPat)
return nullptr;
std::unique_ptr<InstructionPattern> Pat;
if (const DagInit *IP = getDagWithOperatorOfSubClass(Arg, "Instruction")) {
auto &Instr = CGT.getInstruction(IP->getOperatorAsDef(DiagLoc));
Pat =
std::make_unique<CodeGenInstructionPattern>(Instr, insertStrRef(Name));
} else if (const DagInit *IP =
getDagWithOperatorOfSubClass(Arg, "Intrinsic")) {
Record *TheDef = IP->getOperatorAsDef(DiagLoc);
const CodeGenIntrinsic *Intrin = getCodeGenIntrinsic(TheDef);
const CodeGenInstruction &Instr = getInstrForIntrinsic(CGT, Intrin);
Pat =
std::make_unique<CodeGenInstructionPattern>(Instr, insertStrRef(Name));
cast<CodeGenInstructionPattern>(*Pat).setIntrinsic(Intrin);
} else if (const DagInit *PFP =
getDagWithOperatorOfSubClass(Arg, PatFrag::ClassName)) {
const Record *Def = PFP->getOperatorAsDef(DiagLoc);
const PatFrag *PF = parsePatFrag(Def);
if (!PF)
return nullptr; // Already diagnosed by parsePatFrag
Pat = std::make_unique<PatFragPattern>(*PF, insertStrRef(Name));
} else if (const DagInit *BP =
getDagWithOperatorOfSubClass(Arg, BuiltinPattern::ClassName)) {
Pat = std::make_unique<BuiltinPattern>(*BP->getOperatorAsDef(DiagLoc),
insertStrRef(Name));
} else
return nullptr;
for (unsigned K = 0; K < DagPat->getNumArgs(); ++K) {
Init *Arg = DagPat->getArg(K);
if (auto *DagArg = getDagWithSpecificOperator(*Arg, "MIFlags")) {
if (!parseInstructionPatternMIFlags(*Pat, DagArg))
return nullptr;
continue;
}
if (!parseInstructionPatternOperand(*Pat, Arg, DagPat->getArgName(K)))
return nullptr;
}
if (!Pat->checkSemantics(DiagLoc))
return nullptr;
return std::move(Pat);
}
std::unique_ptr<Pattern>
PatternParser::parseWipMatchOpcodeMatcher(const Init &Arg, StringRef Name) {
const DagInit *Matcher = getDagWithSpecificOperator(Arg, "wip_match_opcode");
if (!Matcher)
return nullptr;
if (Matcher->getNumArgs() == 0) {
PrintError(DiagLoc, "Empty wip_match_opcode");
return nullptr;
}
// Each argument is an opcode that can match.
auto Result = std::make_unique<AnyOpcodePattern>(insertStrRef(Name));
for (const auto &Arg : Matcher->getArgs()) {
Record *OpcodeDef = getDefOfSubClass(*Arg, "Instruction");
if (OpcodeDef) {
Result->addOpcode(&CGT.getInstruction(OpcodeDef));
continue;
}
PrintError(DiagLoc, "Arguments to wip_match_opcode must be instructions");
return nullptr;
}
return std::move(Result);
}
bool PatternParser::parseInstructionPatternOperand(InstructionPattern &IP,
const Init *OpInit,
const StringInit *OpName) {
const auto ParseErr = [&]() {
PrintError(DiagLoc,
"cannot parse operand '" + OpInit->getAsUnquotedString() + "' ");
if (OpName)
PrintNote(DiagLoc,
"operand name is '" + OpName->getAsUnquotedString() + '\'');
return false;
};
// untyped immediate, e.g. 0
if (const auto *IntImm = dyn_cast<IntInit>(OpInit)) {
std::string Name = OpName ? OpName->getAsUnquotedString() : "";
IP.addOperand(IntImm->getValue(), insertStrRef(Name), PatternType());
return true;
}
// typed immediate, e.g. (i32 0)
if (const auto *DagOp = dyn_cast<DagInit>(OpInit)) {
if (DagOp->getNumArgs() != 1)
return ParseErr();
const Record *TyDef = DagOp->getOperatorAsDef(DiagLoc);
auto ImmTy = PatternType::get(DiagLoc, TyDef,
"cannot parse immediate '" +
DagOp->getAsUnquotedString() + '\'');
if (!ImmTy)
return false;
if (!IP.hasAllDefs()) {
PrintError(DiagLoc, "out operand of '" + IP.getInstName() +
"' cannot be an immediate");
return false;
}
const auto *Val = dyn_cast<IntInit>(DagOp->getArg(0));
if (!Val)
return ParseErr();
std::string Name = OpName ? OpName->getAsUnquotedString() : "";
IP.addOperand(Val->getValue(), insertStrRef(Name), *ImmTy);
return true;
}
// Typed operand e.g. $x/$z in (G_FNEG $x, $z)
if (auto *DefI = dyn_cast<DefInit>(OpInit)) {
if (!OpName) {
PrintError(DiagLoc, "expected an operand name after '" +
OpInit->getAsString() + '\'');
return false;
}
const Record *Def = DefI->getDef();
auto Ty = PatternType::get(DiagLoc, Def, "cannot parse operand type");
if (!Ty)
return false;
IP.addOperand(insertStrRef(OpName->getAsUnquotedString()), *Ty);
return true;
}
// Untyped operand e.g. $x/$z in (G_FNEG $x, $z)
if (isa<UnsetInit>(OpInit)) {
assert(OpName && "Unset w/ no OpName?");
IP.addOperand(insertStrRef(OpName->getAsUnquotedString()), PatternType());
return true;
}
return ParseErr();
}
bool PatternParser::parseInstructionPatternMIFlags(InstructionPattern &IP,
const DagInit *Op) {
auto *CGIP = dyn_cast<CodeGenInstructionPattern>(&IP);
if (!CGIP) {
PrintError(DiagLoc,
"matching/writing MIFlags is only allowed on CodeGenInstruction "
"patterns");
return false;
}
const auto CheckFlagEnum = [&](const Record *R) {
if (!R->isSubClassOf(MIFlagsEnumClassName)) {
PrintError(DiagLoc, "'" + R->getName() + "' is not a subclass of '" +
MIFlagsEnumClassName + "'");
return false;
}
return true;
};
if (CGIP->getMIFlagsInfo()) {
PrintError(DiagLoc, "MIFlags can only be present once on an instruction");
return false;
}
auto &FI = CGIP->getOrCreateMIFlagsInfo();
for (unsigned K = 0; K < Op->getNumArgs(); ++K) {
const Init *Arg = Op->getArg(K);
// Match/set a flag: (MIFlags FmNoNans)
if (const auto *Def = dyn_cast<DefInit>(Arg)) {
const Record *R = Def->getDef();
if (!CheckFlagEnum(R))
return false;
FI.addSetFlag(R);
continue;
}
// Do not match a flag/unset a flag: (MIFlags (not FmNoNans))
if (const DagInit *NotDag = getDagWithSpecificOperator(*Arg, "not")) {
for (const Init *NotArg : NotDag->getArgs()) {
const DefInit *DefArg = dyn_cast<DefInit>(NotArg);
if (!DefArg) {
PrintError(DiagLoc, "cannot parse '" + NotArg->getAsUnquotedString() +
"': expected a '" + MIFlagsEnumClassName +
"'");
return false;
}
const Record *R = DefArg->getDef();
if (!CheckFlagEnum(R))
return false;
FI.addUnsetFlag(R);
continue;
}
continue;
}
// Copy flags from a matched instruction: (MIFlags $mi)
if (isa<UnsetInit>(Arg)) {
FI.addCopyFlag(insertStrRef(Op->getArgName(K)->getAsUnquotedString()));
continue;
}
}
return true;
}
std::unique_ptr<PatFrag> PatternParser::parsePatFragImpl(const Record *Def) {
auto StackTrace = PrettyStackTraceParse(*Def);
if (!Def->isSubClassOf(PatFrag::ClassName))
return nullptr;
const DagInit *Ins = Def->getValueAsDag("InOperands");
if (Ins->getOperatorAsDef(Def->getLoc())->getName() != "ins") {
PrintError(Def, "expected 'ins' operator for " + PatFrag::ClassName +
" in operands list");
return nullptr;
}
const DagInit *Outs = Def->getValueAsDag("OutOperands");
if (Outs->getOperatorAsDef(Def->getLoc())->getName() != "outs") {
PrintError(Def, "expected 'outs' operator for " + PatFrag::ClassName +
" out operands list");
return nullptr;
}
auto Result = std::make_unique<PatFrag>(*Def);
if (!parsePatFragParamList(*Outs, [&](StringRef Name, unsigned Kind) {
Result->addOutParam(insertStrRef(Name), (PatFrag::ParamKind)Kind);
return true;
}))
return nullptr;
if (!parsePatFragParamList(*Ins, [&](StringRef Name, unsigned Kind) {
Result->addInParam(insertStrRef(Name), (PatFrag::ParamKind)Kind);
return true;
}))
return nullptr;
const ListInit *Alts = Def->getValueAsListInit("Alternatives");
unsigned AltIdx = 0;
for (const Init *Alt : *Alts) {
const auto *PatDag = dyn_cast<DagInit>(Alt);
if (!PatDag) {
PrintError(Def, "expected dag init for PatFrag pattern alternative");
return nullptr;
}
PatFrag::Alternative &A = Result->addAlternative();
const auto AddPat = [&](std::unique_ptr<Pattern> Pat) {
A.Pats.push_back(std::move(Pat));
return true;
};
SaveAndRestore<ArrayRef<SMLoc>> DiagLocSAR(DiagLoc, Def->getLoc());
if (!parsePatternList(
*PatDag, AddPat, "pattern",
/*AnonPatPrefix*/
(Def->getName() + "_alt" + Twine(AltIdx++) + "_pattern").str()))
return nullptr;
}
if (!Result->buildOperandsTables() || !Result->checkSemantics())
return nullptr;
return Result;
}
bool PatternParser::parsePatFragParamList(
const DagInit &OpsList,
function_ref<bool(StringRef, unsigned)> ParseAction) {
for (unsigned K = 0; K < OpsList.getNumArgs(); ++K) {
const StringInit *Name = OpsList.getArgName(K);
const Init *Ty = OpsList.getArg(K);
if (!Name) {
PrintError(DiagLoc, "all operands must be named'");
return false;
}
const std::string NameStr = Name->getAsUnquotedString();
PatFrag::ParamKind OpKind;
if (isSpecificDef(*Ty, "gi_imm"))
OpKind = PatFrag::PK_Imm;
else if (isSpecificDef(*Ty, "root"))
OpKind = PatFrag::PK_Root;
else if (isa<UnsetInit>(Ty) ||
isSpecificDef(*Ty, "gi_mo")) // no type = gi_mo.
OpKind = PatFrag::PK_MachineOperand;
else {
PrintError(
DiagLoc,
'\'' + NameStr +
"' operand type was expected to be 'root', 'gi_imm' or 'gi_mo'");
return false;
}
if (!ParseAction(NameStr, (unsigned)OpKind))
return false;
}
return true;
}
const PatFrag *PatternParser::parsePatFrag(const Record *Def) {
// Cache already parsed PatFrags to avoid doing extra work.
static DenseMap<const Record *, std::unique_ptr<PatFrag>> ParsedPatFrags;
auto It = ParsedPatFrags.find(Def);
if (It != ParsedPatFrags.end()) {
SeenPatFrags.insert(It->second.get());
return It->second.get();
}
std::unique_ptr<PatFrag> NewPatFrag = parsePatFragImpl(Def);
if (!NewPatFrag) {
PrintError(Def, "Could not parse " + PatFrag::ClassName + " '" +
Def->getName() + "'");
// Put a nullptr in the map so we don't attempt parsing this again.
ParsedPatFrags[Def] = nullptr;
return nullptr;
}
const auto *Res = NewPatFrag.get();
ParsedPatFrags[Def] = std::move(NewPatFrag);
SeenPatFrags.insert(Res);
return Res;
}
} // namespace gi
} // namespace llvm
|