|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 declares the CIR dialect attributes. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef LLVM_CLANG_CIR_DIALECT_IR_CIRATTRS_TD |
| 14 | +#define LLVM_CLANG_CIR_DIALECT_IR_CIRATTRS_TD |
| 15 | + |
| 16 | +include "mlir/IR/BuiltinAttributeInterfaces.td" |
| 17 | +include "mlir/IR/EnumAttr.td" |
| 18 | + |
| 19 | +include "clang/CIR/Dialect/IR/CIRDialect.td" |
| 20 | + |
| 21 | +//===----------------------------------------------------------------------===// |
| 22 | +// CIR Attrs |
| 23 | +//===----------------------------------------------------------------------===// |
| 24 | + |
| 25 | +class CIR_Attr<string name, string attrMnemonic, list<Trait> traits = []> |
| 26 | + : AttrDef<CIR_Dialect, name, traits> { |
| 27 | + let mnemonic = attrMnemonic; |
| 28 | +} |
| 29 | + |
| 30 | +class CIRUnitAttr<string name, string attrMnemonic, list<Trait> traits = []> |
| 31 | + : CIR_Attr<name, attrMnemonic, traits> { |
| 32 | + let returnType = "bool"; |
| 33 | + let defaultValue = "false"; |
| 34 | + let valueType = NoneType; |
| 35 | + let isOptional = 1; |
| 36 | +} |
| 37 | + |
| 38 | +//===----------------------------------------------------------------------===// |
| 39 | +// IntegerAttr |
| 40 | +//===----------------------------------------------------------------------===// |
| 41 | + |
| 42 | +def IntAttr : CIR_Attr<"Int", "int", [TypedAttrInterface]> { |
| 43 | + let summary = "An attribute containing an integer value"; |
| 44 | + let description = [{ |
| 45 | + An integer attribute is a literal attribute that represents an integral |
| 46 | + value of the specified integer type. |
| 47 | + }]; |
| 48 | + let parameters = (ins AttributeSelfTypeParameter<"">:$type, |
| 49 | + "llvm::APInt":$value); |
| 50 | + let builders = [ |
| 51 | + AttrBuilderWithInferredContext<(ins "mlir::Type":$type, |
| 52 | + "const llvm::APInt &":$value), [{ |
| 53 | + return $_get(type.getContext(), type, value); |
| 54 | + }]>, |
| 55 | + AttrBuilderWithInferredContext<(ins "mlir::Type":$type, |
| 56 | + "int64_t":$value), [{ |
| 57 | + IntType intType = mlir::cast<IntType>(type); |
| 58 | + mlir::APInt apValue(intType.getWidth(), value, intType.isSigned()); |
| 59 | + return $_get(intType.getContext(), intType, apValue); |
| 60 | + }]>, |
| 61 | + ]; |
| 62 | + let extraClassDeclaration = [{ |
| 63 | + int64_t getSInt() const { return getValue().getSExtValue(); } |
| 64 | + uint64_t getUInt() const { return getValue().getZExtValue(); } |
| 65 | + bool isNullValue() const { return getValue() == 0; } |
| 66 | + uint64_t getBitWidth() const { |
| 67 | + return mlir::cast<IntType>(getType()).getWidth(); |
| 68 | + } |
| 69 | + }]; |
| 70 | + let genVerifyDecl = 1; |
| 71 | + let hasCustomAssemblyFormat = 1; |
| 72 | +} |
| 73 | + |
| 74 | +//===----------------------------------------------------------------------===// |
| 75 | +// FPAttr |
| 76 | +//===----------------------------------------------------------------------===// |
| 77 | + |
| 78 | +def FPAttr : CIR_Attr<"FP", "fp", [TypedAttrInterface]> { |
| 79 | + let summary = "An attribute containing a floating-point value"; |
| 80 | + let description = [{ |
| 81 | + An fp attribute is a literal attribute that represents a floating-point |
| 82 | + value of the specified floating-point type. Supporting only CIR FP types. |
| 83 | + }]; |
| 84 | + let parameters = (ins |
| 85 | + AttributeSelfTypeParameter<"", "::cir::CIRFPTypeInterface">:$type, |
| 86 | + APFloatParameter<"">:$value |
| 87 | + ); |
| 88 | + let builders = [ |
| 89 | + AttrBuilderWithInferredContext<(ins "mlir::Type":$type, |
| 90 | + "const llvm::APFloat &":$value), [{ |
| 91 | + return $_get(type.getContext(), mlir::cast<CIRFPTypeInterface>(type), |
| 92 | + value); |
| 93 | + }]>, |
| 94 | + AttrBuilder<(ins "mlir::Type":$type, |
| 95 | + "const llvm::APFloat &":$value), [{ |
| 96 | + return $_get($_ctxt, mlir::cast<CIRFPTypeInterface>(type), value); |
| 97 | + }]>, |
| 98 | + ]; |
| 99 | + let extraClassDeclaration = [{ |
| 100 | + static FPAttr getZero(mlir::Type type); |
| 101 | + }]; |
| 102 | + let genVerifyDecl = 1; |
| 103 | + |
| 104 | + let assemblyFormat = [{ |
| 105 | + `<` custom<FloatLiteral>($value, ref($type)) `>` |
| 106 | + }]; |
| 107 | +} |
| 108 | + |
| 109 | +//===----------------------------------------------------------------------===// |
| 110 | +// ConstPtrAttr |
| 111 | +//===----------------------------------------------------------------------===// |
| 112 | + |
| 113 | +def ConstPtrAttr : CIR_Attr<"ConstPtr", "ptr", [TypedAttrInterface]> { |
| 114 | + let summary = "Holds a constant pointer value"; |
| 115 | + let parameters = (ins |
| 116 | + AttributeSelfTypeParameter<"", "::cir::PointerType">:$type, |
| 117 | + "mlir::IntegerAttr":$value); |
| 118 | + let description = [{ |
| 119 | + A pointer attribute is a literal attribute that represents an integral |
| 120 | + value of a pointer type. |
| 121 | + }]; |
| 122 | + let builders = [ |
| 123 | + AttrBuilderWithInferredContext<(ins "mlir::Type":$type, |
| 124 | + "mlir::IntegerAttr":$value), [{ |
| 125 | + return $_get(type.getContext(), mlir::cast<cir::PointerType>(type), |
| 126 | + value); |
| 127 | + }]>, |
| 128 | + AttrBuilder<(ins "mlir::Type":$type, |
| 129 | + "mlir::IntegerAttr":$value), [{ |
| 130 | + return $_get($_ctxt, mlir::cast<cir::PointerType>(type), value); |
| 131 | + }]>, |
| 132 | + ]; |
| 133 | + let extraClassDeclaration = [{ |
| 134 | + bool isNullValue() const { return getValue().getInt() == 0; } |
| 135 | + }]; |
| 136 | + |
| 137 | + let assemblyFormat = [{ |
| 138 | + `<` custom<ConstPtr>($value) `>` |
| 139 | + }]; |
| 140 | +} |
| 141 | + |
| 142 | +#endif // LLVM_CLANG_CIR_DIALECT_IR_CIRATTRS_TD |
0 commit comments