Assigning a constant to a standard operation


I am trying to lower operations from one dialect to the standard dialect

In a Tablegen file, I have this piece of code :

def fixObscureTypeMismatch : NativeCodeCall<"$0">;
def LowerEqPattern : Pat<
    (EqExpr $lhs,$rhs),
    (fixObscureTypeMismatch (CmpIOp 0, $lhs, $rhs))>;

When building, I get this error :

[main] Building folder: cpp 
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/lam/ETHZ/rsc/bsc-fiebigm/cpp/build --config Debug --target all -j 18 --
[build] [1/1 100% :: 0.015] Building
[build] FAILED: 
[build] cd /home/lam/ETHZ/rsc/bsc-fiebigm/cpp/build && /opt/clang+llvm-12.0.0git/bin/mlir-tblgen -gen-rewriters -I/home/lam/ETHZ/rsc/bsc-fiebigm/cpp/src -I /home/lam/ETHZ/rsc/bsc-fiebigm/cpp -I/opt/clang+llvm-12.0.0git/include -I/opt/clang+llvm-12.0.0git/include /home/lam/ETHZ/rsc/bsc-fiebigm/cpp/src/ --write-if-changed -o -d
[build] /home/lam/ETHZ/rsc/bsc-fiebigm/cpp/src/ error: referencing unbound symbol ''
[build] def LowerEqPattern : Pat<
[build] ^
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1

So I assume that I cannot write 0 as the first operand directly. I would like to specify that the EqExpr should lower to CmpIOp with the predicate set to 0 or “eq”. How do I do this?

Not sure why you’d need the fixObscureTypeMismatch? I’d assume the source EqExpr should generate boolean values, which is the same as CmpIOp?

Note that what you write in the resultant patterns are redirected to the underlying C++ build method for the corresponding op to construct. The build method of CmpIOp is expecting attributes and SSA values. When the attribute is an enum attribute, you can directly specify the enum case there. So maybe the following could work for you:

def LowerEqPattern : Pat<
  (EqExpr $lhs, $rhs),
  (CmpIOp CMPI_P_EQ, $lhs, $rhs)