MLIR  19.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 /// Maps arithmetic fastmath enum values to LLVM enum values.
22 LLVM::FastmathFlags
23 convertArithFastMathFlagsToLLVM(arith::FastMathFlags arithFMF);
24 
25 /// Creates an LLVM fastmath attribute from a given arithmetic fastmath
26 /// attribute.
27 LLVM::FastmathFlagsAttr
28 convertArithFastMathAttrToLLVM(arith::FastMathFlagsAttr fmfAttr);
29 
30 /// Maps arithmetic overflow enum values to LLVM enum values.
31 LLVM::IntegerOverflowFlags
32 convertArithOverflowFlagsToLLVM(arith::IntegerOverflowFlags arithFlags);
33 
34 /// Creates an LLVM overflow attribute from a given arithmetic overflow
35 /// attribute.
36 LLVM::IntegerOverflowFlagsAttr
37 convertArithOverflowAttrToLLVM(arith::IntegerOverflowFlagsAttr flagsAttr);
38 
39 // Attribute converter that populates a NamedAttrList by removing the fastmath
40 // attribute from the source operation attributes, and replacing it with an
41 // equivalent LLVM fastmath attribute.
42 template <typename SourceOp, typename TargetOp>
44 public:
45  AttrConvertFastMathToLLVM(SourceOp srcOp) {
46  // Copy the source attributes.
47  convertedAttr = NamedAttrList{srcOp->getAttrs()};
48  // Get the name of the arith fastmath attribute.
49  StringRef arithFMFAttrName = SourceOp::getFastMathAttrName();
50  // Remove the source fastmath attribute.
51  auto arithFMFAttr = dyn_cast_if_present<arith::FastMathFlagsAttr>(
52  convertedAttr.erase(arithFMFAttrName));
53  if (arithFMFAttr) {
54  StringRef targetAttrName = TargetOp::getFastmathAttrName();
55  convertedAttr.set(targetAttrName,
56  convertArithFastMathAttrToLLVM(arithFMFAttr));
57  }
58  }
59 
60  ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
61 
62 private:
63  NamedAttrList convertedAttr;
64 };
65 
66 // Attribute converter that populates a NamedAttrList by removing the overflow
67 // attribute from the source operation attributes, and replacing it with an
68 // equivalent LLVM overflow attribute.
69 template <typename SourceOp, typename TargetOp>
71 public:
72  AttrConvertOverflowToLLVM(SourceOp srcOp) {
73  // Copy the source attributes.
74  convertedAttr = NamedAttrList{srcOp->getAttrs()};
75  // Get the name of the arith overflow attribute.
76  StringRef arithAttrName = SourceOp::getIntegerOverflowAttrName();
77  // Remove the source overflow attribute.
78  auto arithAttr = dyn_cast_if_present<arith::IntegerOverflowFlagsAttr>(
79  convertedAttr.erase(arithAttrName));
80  if (arithAttr) {
81  StringRef targetAttrName = TargetOp::getIntegerOverflowAttrName();
82  convertedAttr.set(targetAttrName,
84  }
85  }
86 
87  ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
88 
89 private:
90  NamedAttrList convertedAttr;
91 };
92 } // namespace arith
93 } // namespace mlir
94 
95 #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::IntegerOverflowFlagsAttr convertArithOverflowAttrToLLVM(arith::IntegerOverflowFlagsAttr flagsAttr)
Creates an LLVM overflow attribute from a given arithmetic overflow attribute.
LLVM::FastmathFlagsAttr convertArithFastMathAttrToLLVM(arith::FastMathFlagsAttr fmfAttr)
Creates an LLVM fastmath attribute from a given arithmetic fastmath attribute.
LLVM::IntegerOverflowFlags convertArithOverflowFlagsToLLVM(arith::IntegerOverflowFlags arithFlags)
Maps arithmetic overflow enum values to LLVM enum values.
LLVM::FastmathFlags convertArithFastMathFlagsToLLVM(arith::FastMathFlags arithFMF)
Maps arithmetic fastmath enum values to LLVM enum values.
Include the generated interface declarations.