[llvm-dev] [EXT] Please expose predicates to MachineVerifier

Björn Pettersson A via llvm-dev llvm-dev at lists.llvm.org
Wed Apr 24 14:48:02 PDT 2019

Hi Mark!

Downstream I've added some support for our target (in this example called Qwerty) like this.

In utils/TableGen/InstrInfoEmitter.cpp I've added:

void EmitPredicateVerifier(RecordKeeper &Records, raw_ostream &OS) {
  CodeGenTarget Target(Records);
  StringRef NameSpace = Target.getInstNamespace();

  // The generated VerifyPredicates method is only guaranteed to work for
  // Qwerty (it takes a QwerySubtarget* as input). So avoid generating it for
  // other targets.
  if (Target.getName() != "Qwerty")

  OS << "namespace llvm {\n";
  OS << "namespace " << NameSpace << " {\n\n";

  OS << "// "<< "VerifyPredicates" << "\nLLVM_READONLY\n";
  OS << "static bool "<< "VerifyPredicates"
     << "(uint16_t Opcode, const QwertySubtarget *Subtarget) {\n";
  OS << "  switch (Opcode) {\n";

  unsigned Num = 0;
  for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
    std::vector<Record *> PredicateRecs =
    // This is more or less copied from PatternToMatch::getPredicateCheck(),
    // so let's sort the checks in the same way as it is done for the DAGIsel
    // matcher.
    std::sort(PredicateRecs.begin(), PredicateRecs.end(), LessRecord());

    SmallString<128> PredicateCheck;
    for (Record *Pred : PredicateRecs) {
      if (!PredicateCheck.empty())
        PredicateCheck += " && ";
      PredicateCheck += "(";
      PredicateCheck += Pred->getValueAsString("CondString");
      PredicateCheck += ")";
    if (!PredicateCheck.empty()) {
      OS << "  case " << Num << ": /* " << Inst->TheDef->getName() << " */";
      OS << "    return " << PredicateCheck << ";\n";

  OS << "  }\n";
  OS << "  return true;\n";
  OS << "}\n\n";
  OS << "} // End " << NameSpace << " namespace\n";
  OS << "} // End llvm namespace\n";
  OS << "#endif // GET_PREDICATE_VERIFIER\n\n";

And at the end of the same file it is plugged in like this:

namespace llvm {

void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
  EmitMapTable(RK, OS);
+  EmitPredicateVerifier(RK, OS);

And then in QwertyInstrInfo we do:

bool QwertyInstrInfo::verifyInstruction(const MachineInstr &MI,
                                         StringRef &ErrInfo) const {
  if (!QWERTY::VerifyPredicates(MI.getOpcode(), &Subtarget)) {
    ErrInfo = "Instruction is not available for the subtarget.";
    return false;

If this is of interest for other targets (also in-tree) I guess it could be generalized a little bit (as you see above I've hardcoded some parts to use "QwertySubtarget"), and maybe it also can be plugged in to be called from MachineVerifier directly.

The function generated by tablegen will look something like this:

namespace llvm {
namespace QWERTY {

// VerifyPredicates
static bool VerifyPredicates(uint16_t Opcode, const QwertySubtarget *Subtarget) {
  switch (Opcode) {
  case 183: /* add_only_v1_pseudo */    return (Subtarget->isQwertyV1());
  case 195: /* add_only_v2_pseudo */    return (Subtarget->isQwertyV2());
  case 296: /* xor_only_v2 */    return (Subtarget->isQwertyV2());
  return true;

} // End QWERTY namespace
} // End llvm namespace

Feel free to create a patch from the above on your own. Or let me know if there is interest in having something similar to the above added for in-tree targets, and then I can try to find time to generalize my patch.


From: llvm-dev <llvm-dev-bounces at lists.llvm.org> on behalf of Eli Friedman via llvm-dev <llvm-dev at lists.llvm.org>
Sent: Monday, April 1, 2019 8:33:26 PM
To: Mark Schimmel; llvm-dev
Subject: Re: [llvm-dev] [EXT]  Please expose predicates to MachineVerifier

There’s already an existing hook for adding target-specific verification logic in the target’s TargetInstrInfo::verifyInstruction.  That doesn’t really scale to checking that you’re using an appropriate subtarget for every instruction, though.  (You could, but it would involve a lot of redundant hand-written code for many targets.)

We can’t really use any of the existing code generated by TableGen to perform the checks in question; I don’t think we currently expose the relevant predicates in any useful way.  But it would probably be appropriate to extend TableGen to generate code for those checks, I think.  It would probably also require some modifications to the TableGen files themselves; currently, at least for some targets, we use Requires predicates on instructions which are legal, but we don’t want the TableGen’ed isel to select for some reason.


From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Mark Schimmel via llvm-dev
Sent: Monday, April 1, 2019 11:21 AM
To: llvm-dev at lists.llvm.org
Subject: [EXT] [llvm-dev] Please expose predicates to MachineVerifier

Could we expose predicates defined in the target InstrInfo.td file to the MachineVerifier? We use BuildMI() to create many instructions after ISEL, but the predicates are not being checked at this point. Thus, I could forget to check the target and build an instruction that is illegal for a specific configuration. In such a case it would be nice if the MachineVerifier could detect this for me.

Example predicate IsCore8 usage:

def ADDx: BINOP<…>, Requires<[IsCore8]>;

let Predicates=[IsCore8] in {
def : SUBX: BINOP<…>;

The predicates are encoded into the targets GenDAGIsel file but not anywhere else that I can find:
  OPC_CheckPatternPredicate, 4, // (Subtarget.isCore8())


More information about the llvm-dev mailing list