|
| 1 | +//===-- Utils.td - MLIR LLVM IR utilities file -------------*- tablegen -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file contains utilities to map from MLIR LLVM IR dialect to LLVM IR. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef MLIR_DIALECT_LLVMIR_UTILS_TD |
| 14 | +#define MLIR_DIALECT_LLVMIR_UTILS_TD |
| 15 | + |
| 16 | +include "mlir/IR/EnumAttr.td" |
| 17 | + |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | +// Base classes for LLVM enum attributes. |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | + |
| 22 | +// Case of the LLVM enum attribute backed by I64Attr with customized string |
| 23 | +// representation that corresponds to what is visible in the textual IR form. |
| 24 | +// The parameters are as follows: |
| 25 | +// - `cppSym`: name of the C++ enumerant for this case in MLIR API; |
| 26 | +// - `irSym`: keyword used in the custom form of MLIR operation; |
| 27 | +// - `llvmSym`: name of the C++ enumerant for this case in LLVM API. |
| 28 | +// For example, `LLVM_EnumAttrCase<"Weak", "weak", "WeakAnyLinkage">` is usable |
| 29 | +// as `<MlirEnumName>::Weak` in MLIR API, `WeakAnyLinkage` in LLVM API and |
| 30 | +// is printed/parsed as `weak` in MLIR custom textual format. |
| 31 | +class LLVM_EnumAttrCase<string cppSym, string irSym, string llvmSym, int val> : |
| 32 | + I64EnumAttrCase<cppSym, val, irSym> { |
| 33 | + // The name of the equivalent enumerant in LLVM. |
| 34 | + string llvmEnumerant = llvmSym; |
| 35 | +} |
| 36 | + |
| 37 | +// LLVM enum attribute backed by I64Attr with string representation |
| 38 | +// corresponding to what is visible in the textual IR form. |
| 39 | +// The parameters are as follows: |
| 40 | +// - `name`: name of the C++ enum class in MLIR API; |
| 41 | +// - `llvmName`: name of the C++ enum in LLVM API; |
| 42 | +// - `description`: textual description for documentation purposes; |
| 43 | +// - `cases`: list of enum cases; |
| 44 | +// - `unsupportedCases`: optional list of unsupported enum cases. |
| 45 | +// For example, `LLVM_EnumAttr<Linkage, "::llvm::GlobalValue::LinkageTypes` |
| 46 | +// produces `mlir::LLVM::Linkage` enum class in MLIR API that corresponds to (a |
| 47 | +// subset of) values in the `llvm::GlobalValue::LinkageTypes` in LLVM API. |
| 48 | +// All unsupported cases are excluded from the MLIR enum and trigger an error |
| 49 | +// during the import from LLVM IR. They are useful to handle sentinel values |
| 50 | +// such as `llvm::AtomicRMWInst::BinOp::BAD_BINOP` that LLVM commonly uses to |
| 51 | +// terminate its enums. |
| 52 | +class LLVM_EnumAttr<string name, string llvmName, string description, |
| 53 | + list<LLVM_EnumAttrCase> cases, |
| 54 | + list<LLVM_EnumAttrCase> unsupportedCases = []> : |
| 55 | + I64EnumAttr<name, description, cases> { |
| 56 | + // List of unsupported cases that have no conversion to an MLIR value. |
| 57 | + list<LLVM_EnumAttrCase> unsupported = unsupportedCases; |
| 58 | + |
| 59 | + // The equivalent enum class name in LLVM. |
| 60 | + string llvmClassName = llvmName; |
| 61 | +} |
| 62 | + |
| 63 | +// LLVM_CEnumAttr is functionally identical to LLVM_EnumAttr, but to be used for |
| 64 | +// non-class enums. |
| 65 | +class LLVM_CEnumAttr<string name, string llvmNS, string description, |
| 66 | + list<LLVM_EnumAttrCase> cases> : |
| 67 | + I64EnumAttr<name, description, cases> { |
| 68 | + string llvmClassName = llvmNS; |
| 69 | +} |
| 70 | + |
| 71 | +#endif // MLIR_DIALECT_LLVMIR_UTILS_TD |
0 commit comments