1//===- VScaleAttr.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 pass adds a `vscale_range` attribute to function definitions.
12/// The attribute is used for scalable vector operations on Arm processors
13/// and should only be run on processors that support this feature. [It is
14/// likely harmless to run it on something else, but it is also not valuable].
15//===----------------------------------------------------------------------===//
16
17#include "flang/ISO_Fortran_binding_wrapper.h"
18#include "flang/Optimizer/Builder/BoxValue.h"
19#include "flang/Optimizer/Builder/FIRBuilder.h"
20#include "flang/Optimizer/Builder/Runtime/Inquiry.h"
21#include "flang/Optimizer/Dialect/FIRDialect.h"
22#include "flang/Optimizer/Dialect/FIROps.h"
23#include "flang/Optimizer/Dialect/FIRType.h"
24#include "flang/Optimizer/Dialect/Support/FIRContext.h"
25#include "flang/Optimizer/Dialect/Support/KindMapping.h"
26#include "flang/Optimizer/Transforms/Passes.h"
27#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
28#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
29#include "mlir/IR/Matchers.h"
30#include "mlir/IR/TypeUtilities.h"
31#include "mlir/Pass/Pass.h"
32#include "mlir/Transforms/DialectConversion.h"
33#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
34#include "mlir/Transforms/RegionUtils.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/raw_ostream.h"
37
38#include <algorithm>
39
40namespace fir {
41#define GEN_PASS_DECL_VSCALEATTR
42#define GEN_PASS_DEF_VSCALEATTR
43#include "flang/Optimizer/Transforms/Passes.h.inc"
44} // namespace fir
45
46#define DEBUG_TYPE "vscale-attr"
47
48namespace {
49
50class VScaleAttrPass : public fir::impl::VScaleAttrBase<VScaleAttrPass> {
51public:
52 VScaleAttrPass(const fir::VScaleAttrOptions &options) {
53 vscaleRange = options.vscaleRange;
54 }
55 VScaleAttrPass() {}
56 void runOnOperation() override;
57};
58
59} // namespace
60
61void VScaleAttrPass::runOnOperation() {
62 LLVM_DEBUG(llvm::dbgs() << "=== Begin " DEBUG_TYPE " ===\n");
63 mlir::func::FuncOp func = getOperation();
64
65 LLVM_DEBUG(llvm::dbgs() << "Func-name:" << func.getSymName() << "\n");
66
67 auto context = &getContext();
68
69 auto intTy = mlir::IntegerType::get(context, 32);
70
71 assert(vscaleRange.first && "VScaleRange minimum should be non-zero");
72
73 func->setAttr("vscale_range",
74 mlir::LLVM::VScaleRangeAttr::get(
75 context, mlir::IntegerAttr::get(intTy, vscaleRange.first),
76 mlir::IntegerAttr::get(intTy, vscaleRange.second)));
77
78 LLVM_DEBUG(llvm::dbgs() << "=== End " DEBUG_TYPE " ===\n");
79}
80
81std::unique_ptr<mlir::Pass>
82fir::createVScaleAttrPass(std::pair<unsigned, unsigned> vscaleAttr) {
83 VScaleAttrOptions opts;
84 opts.vscaleRange = vscaleAttr;
85 return std::make_unique<VScaleAttrPass>(opts);
86}
87
88std::unique_ptr<mlir::Pass> fir::createVScaleAttrPass() {
89 return std::make_unique<VScaleAttrPass>();
90}
91

source code of flang/lib/Optimizer/Transforms/VScaleAttr.cpp