This is an archive of the discontinued LLVM Phabricator instance.

[TableGen] Add "MCInstValidatorEmitter" TableGen backend
AbandonedPublic

Authored by nlguillemot on Jun 15 2019, 1:18 AM.

Details

Summary

There are many places in the compiler that generate MCInsts, and these different places generally impose similar rules on their generated MCInsts. For example, an operand in a given ISA might represent a special flag that only supports a certain set of possible values, and all places that create MCInsts (parser, disassembler, etc) should respect the restrictions of that flag. If we can ensure that the MCInsts outputted by various parts of the compiler respect their restrictions, we might be able to catch bugs more easily.

At first glance, the pre-existing MCOperandPredicate member of TableGen's base Operand class looks like it should be the one to fill the role of a convenient place to describe the restrictions on operands. However, this MCOperandPredicate member is currently mostly unused by LLVM. As a step towards making it easier to implement validation of MCInsts in various places of the compiler, this patch adds a new TableGen backend called MCInstValidatorEmitter. This backend exposes a function called validateMCOperand, which can be used to conveniently check the validity of an operand of an MCInst.

As future work, this backend could be enhanced with more functions to validate MCInsts. For example, we could add another function to check the validity of an MCInst as a whole, by giving MCInsts a MCInstPredicate (or similar.)

A note about the many "predicate"s in LLVM:
The word "predicate" is quite overloaded, since it is also used to describe parts of the functionality of the scheduler, parser, decoder, etc. For this backend, the "predicate" is not related to a specific part of the compiler. It's just a general purpose function that can be used to validate that a given MCInst respects the rules of its operands. By being general-purpose, it becomes possible to use this API to validate the MCInsts produced in any given part in the compiler. Being able to describe the validity of the values of an operand type in just one place is what makes this interesting.

Diff Detail

Event Timeline

nlguillemot created this revision.Jun 15 2019, 1:18 AM
nlguillemot added inline comments.Jul 18 2019, 10:13 AM
utils/TableGen/MCInstValidatorEmitter.cpp
61

bug: If the value is already present in the SetVector, the MCOperandPredicateID should be set to its existing ID. As-is, it creates a new ID even when the value should be reused. Should be able to reproduce the bug by updating the unit test to have two instructions that share the same MCOperandPredicate.

nlguillemot abandoned this revision.Apr 26 2020, 4:15 PM

Abandoning this project.