| 1 |
//===- FunctionAttr.cpp ---------------------------------------------------===// |
| 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 |
//===----------------------------------------------------------------------===// |
| 10 |
/// \file |
| 11 |
/// This is a generic pass for adding attributes to functions. |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
#include "flang/Optimizer/Transforms/Passes.h" |
| 14 |
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
| 15 |
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 16 |
|
| 17 |
namespace fir { |
| 18 |
#define GEN_PASS_DEF_FUNCTIONATTR |
| 19 |
#include "flang/Optimizer/Transforms/Passes.h.inc" |
| 20 |
} // namespace fir |
| 21 |
|
| 22 |
#define DEBUG_TYPE "func-attr" |
| 23 |
|
| 24 |
namespace { |
| 25 |
|
| 26 |
class FunctionAttrPass : public fir::impl::FunctionAttrBase<FunctionAttrPass> { |
| 27 |
public: |
| 28 |
FunctionAttrPass(const fir::FunctionAttrOptions &options) { |
| 29 |
framePointerKind = options.framePointerKind; |
| 30 |
noInfsFPMath = options.noInfsFPMath; |
| 31 |
noNaNsFPMath = options.noNaNsFPMath; |
| 32 |
approxFuncFPMath = options.approxFuncFPMath; |
| 33 |
noSignedZerosFPMath = options.noSignedZerosFPMath; |
| 34 |
unsafeFPMath = options.unsafeFPMath; |
| 35 |
} |
| 36 |
FunctionAttrPass() {} |
| 37 |
void runOnOperation() override; |
| 38 |
}; |
| 39 |
|
| 40 |
} // namespace |
| 41 |
|
| 42 |
void FunctionAttrPass::runOnOperation() { |
| 43 |
LLVM_DEBUG(llvm::dbgs() << "=== Begin " DEBUG_TYPE " ===\n"); |
| 44 |
mlir::func::FuncOp func = getOperation(); |
| 45 |
|
| 46 |
LLVM_DEBUG(llvm::dbgs() << "Func-name:" << func.getSymName() << "\n"); |
| 47 |
|
| 48 |
mlir::MLIRContext *context = &getContext(); |
| 49 |
if (framePointerKind != mlir::LLVM::framePointerKind::FramePointerKind::None) |
| 50 |
func->setAttr("frame_pointer", mlir::LLVM::FramePointerKindAttr::get( |
| 51 |
context, framePointerKind)); |
| 52 |
|
| 53 |
auto llvmFuncOpName = |
| 54 |
mlir::OperationName(mlir::LLVM::LLVMFuncOp::getOperationName(), context); |
| 55 |
if (noInfsFPMath) |
| 56 |
func->setAttr( |
| 57 |
mlir::LLVM::LLVMFuncOp::getNoInfsFpMathAttrName(llvmFuncOpName), |
| 58 |
mlir::BoolAttr::get(context, true)); |
| 59 |
if (noNaNsFPMath) |
| 60 |
func->setAttr( |
| 61 |
mlir::LLVM::LLVMFuncOp::getNoNansFpMathAttrName(llvmFuncOpName), |
| 62 |
mlir::BoolAttr::get(context, true)); |
| 63 |
if (approxFuncFPMath) |
| 64 |
func->setAttr( |
| 65 |
mlir::LLVM::LLVMFuncOp::getApproxFuncFpMathAttrName(llvmFuncOpName), |
| 66 |
mlir::BoolAttr::get(context, true)); |
| 67 |
if (noSignedZerosFPMath) |
| 68 |
func->setAttr( |
| 69 |
mlir::LLVM::LLVMFuncOp::getNoSignedZerosFpMathAttrName(llvmFuncOpName), |
| 70 |
mlir::BoolAttr::get(context, true)); |
| 71 |
if (unsafeFPMath) |
| 72 |
func->setAttr( |
| 73 |
mlir::LLVM::LLVMFuncOp::getUnsafeFpMathAttrName(llvmFuncOpName), |
| 74 |
mlir::BoolAttr::get(context, true)); |
| 75 |
|
| 76 |
LLVM_DEBUG(llvm::dbgs() << "=== End " DEBUG_TYPE " ===\n"); |
| 77 |
} |