Question on creating new pragma and code generation for duplication/comparison of a variable


I’m a newbie and want to extend LLVM for my project.
I’ll appreciate any help.
(I thought I posted similar post to llvm-dev mailing list, but it didn’t seem to be posted at that time.)

The goal of the project is to provide software fault tolerance using LLVM compiler.
With pragma, a user can specify which variables to be protected (maybe by duplicating and comparing.) within a region.
The general idea is to annotate the code with the information of the variables to be protected.

pragma fault_tolerance protect(a)

a = b * c;

d = a + b;

Clang compiles it into the following code semantically.

a = b * c;
_save_copy(a, loc1, loc2);

_retrieve_and_compare(a, loc1, loc2);
d = a + b;

I’m using OpenMP implementation as a reference.
As an initial test, I’m implementing it as a new OpenMP directive.
In OpenMP implementation, I’m using OMP_Parallel_Directive as the reference.
With the pragma, OMP_Fault_Tolerance_Directive is created and variable ‘a’ information is kept.
I’m trying to find the variable(s) used in the region and inject a new statement.
I’m having trouble how to check if a statement has a matching variable as either LHS or RHS.
I saw there are lots of matcher routines including ‘AST_POLYMORPHIC_MATCHER_P()’.
However, I cannot find an example of its usage in the compiler code.
Any help for that?


We don’t typically use AST matchers within the compiler itself (they tend to be used by tools like clang-tidy rather than internally) because the compiler usually has sufficient information to not need a matcher. In this case, the parser will parse the pragma (annotated tokens coming from the preprocessor) and eventually will call an ActOnWhatever() handler in Sema to build the AST node. Most of this code lives in ParseOpenMP.cpp and SemaOpenMP.cpp – I’d recommend looking at how that code is structured and go from there.

Thank you very much.
I’m looking into those files, but could not find the right routines, yet.
I think I need to get myself familiar to the code more before I can discover what I need to use.


1 Like