MLIR  17.0.0git
AttrToLLVMConverter.h
Go to the documentation of this file.
1 //===- AttrToLLVMConverter.h - Arith attributes conversion ------*- C++ -*-===//
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 #ifndef MLIR_CONVERSION_ARITHCOMMON_ATTRTOLLVMCONVERTER_H
10 #define MLIR_CONVERSION_ARITHCOMMON_ATTRTOLLVMCONVERTER_H
11 
14 
15 //===----------------------------------------------------------------------===//
16 // Support for converting Arith FastMathFlags to LLVM FastmathFlags
17 //===----------------------------------------------------------------------===//
18 
19 namespace mlir {
20 namespace arith {
21 // Map arithmetic fastmath enum values to LLVMIR enum values.
22 LLVM::FastmathFlags
23 convertArithFastMathFlagsToLLVM(arith::FastMathFlags arithFMF);
24 
25 // Create an LLVM fastmath attribute from a given arithmetic fastmath attribute.
26 LLVM::FastmathFlagsAttr
27 convertArithFastMathAttrToLLVM(arith::FastMathFlagsAttr fmfAttr);
28 
29 // Attribute converter that populates a NamedAttrList by removing the fastmath
30 // attribute from the source operation attributes, and replacing it with an
31 // equivalent LLVM fastmath attribute.
32 template <typename SourceOp, typename TargetOp>
34 public:
35  AttrConvertFastMathToLLVM(SourceOp srcOp) {
36  // Copy the source attributes.
37  convertedAttr = NamedAttrList{srcOp->getAttrs()};
38  // Get the name of the arith fastmath attribute.
39  llvm::StringRef arithFMFAttrName = SourceOp::getFastMathAttrName();
40  // Remove the source fastmath attribute.
41  auto arithFMFAttr =
42  convertedAttr.erase(arithFMFAttrName)
43  .template dyn_cast_or_null<arith::FastMathFlagsAttr>();
44  if (arithFMFAttr) {
45  llvm::StringRef targetAttrName = TargetOp::getFastmathAttrName();
46  convertedAttr.set(targetAttrName,
47  convertArithFastMathAttrToLLVM(arithFMFAttr));
48  }
49  }
50 
51  ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
52 
53 private:
54  NamedAttrList convertedAttr;
55 };
56 
57 // Attribute converter that populates a NamedAttrList by removing the fastmath
58 // attribute from the source operation attributes. This may be useful for
59 // target operations that do not require the fastmath attribute, or for targets
60 // that do not yet support the LLVM fastmath attribute.
61 template <typename SourceOp, typename TargetOp>
63 public:
64  AttrDropFastMath(SourceOp srcOp) {
65  // Copy the source attributes.
66  convertedAttr = NamedAttrList{srcOp->getAttrs()};
67  // Get the name of the arith fastmath attribute.
68  llvm::StringRef arithFMFAttrName = SourceOp::getFastMathAttrName();
69  // Remove the source fastmath attribute.
70  convertedAttr.erase(arithFMFAttrName);
71  }
72 
73  ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
74 
75 private:
76  NamedAttrList convertedAttr;
77 };
78 } // namespace arith
79 } // namespace mlir
80 
81 #endif // MLIR_CONVERSION_ARITHCOMMON_ATTRTOLLVMCONVERTER_H
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
ArrayRef< NamedAttribute > getAttrs() const
Return all of the attributes on this operation.
Attribute erase(StringAttr name)
Erase the attribute with the given name from the list.
Attribute set(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
ArrayRef< NamedAttribute > getAttrs() const
ArrayRef< NamedAttribute > getAttrs() const
LLVM::FastmathFlagsAttr convertArithFastMathAttrToLLVM(arith::FastMathFlagsAttr fmfAttr)
LLVM::FastmathFlags convertArithFastMathFlagsToLLVM(arith::FastMathFlags arithFMF)
Include the generated interface declarations.