1 //===-- Optimizer/Dialect/FIROpsSupport.h -- FIR op support -----*- 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 OPTIMIZER_DIALECT_FIROPSSUPPORT_H
10 #define OPTIMIZER_DIALECT_FIROPSSUPPORT_H
11
12 #include "flang/Optimizer/Dialect/FIROps.h"
13 #include "mlir/Dialect/StandardOps/IR/Ops.h"
14 #include "mlir/IR/BuiltinOps.h"
15
16 namespace fir {
17
18 /// return true iff the Operation is a non-volatile LoadOp
nonVolatileLoad(mlir::Operation * op)19 inline bool nonVolatileLoad(mlir::Operation *op) {
20 if (auto load = dyn_cast<fir::LoadOp>(op))
21 return !load.getAttr("volatile");
22 return false;
23 }
24
25 /// return true iff the Operation is a call
isaCall(mlir::Operation * op)26 inline bool isaCall(mlir::Operation *op) {
27 return isa<fir::CallOp>(op) || isa<fir::DispatchOp>(op) ||
28 isa<mlir::CallOp>(op) || isa<mlir::CallIndirectOp>(op);
29 }
30
31 /// return true iff the Operation is a fir::CallOp, fir::DispatchOp,
32 /// mlir::CallOp, or mlir::CallIndirectOp and not pure
33 /// NB: this is not the same as `!pureCall(op)`
impureCall(mlir::Operation * op)34 inline bool impureCall(mlir::Operation *op) {
35 // Should we also auto-detect that the called function is pure if its
36 // arguments are not references? For now, rely on a "pure" attribute.
37 return op && isaCall(op) && !op->getAttr("pure");
38 }
39
40 /// return true iff the Operation is a fir::CallOp, fir::DispatchOp,
41 /// mlir::CallOp, or mlir::CallIndirectOp and is also pure.
42 /// NB: this is not the same as `!impureCall(op)`
pureCall(mlir::Operation * op)43 inline bool pureCall(mlir::Operation *op) {
44 // Should we also auto-detect that the called function is pure if its
45 // arguments are not references? For now, rely on a "pure" attribute.
46 return op && isaCall(op) && op->getAttr("pure");
47 }
48
49 /// Get or create a FuncOp in a module.
50 ///
51 /// If `module` already contains FuncOp `name`, it is returned. Otherwise, a new
52 /// FuncOp is created, and that new FuncOp is returned.
53 mlir::FuncOp createFuncOp(mlir::Location loc, mlir::ModuleOp module,
54 llvm::StringRef name, mlir::FunctionType type,
55 llvm::ArrayRef<mlir::NamedAttribute> attrs = {});
56
57 /// Get or create a GlobalOp in a module.
58 fir::GlobalOp createGlobalOp(mlir::Location loc, mlir::ModuleOp module,
59 llvm::StringRef name, mlir::Type type,
60 llvm::ArrayRef<mlir::NamedAttribute> attrs = {});
61
62 } // namespace fir
63
64 #endif // OPTIMIZER_DIALECT_FIROPSSUPPORT_H
65