xref: /llvm-project/flang/lib/Optimizer/Transforms/VScaleAttr.cpp (revision 8f1164948d6b8f3d3ea11d0ee0629af82fe8cb74)
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 
40 namespace fir {
41 #define GEN_PASS_DEF_VSCALEATTR
42 #include "flang/Optimizer/Transforms/Passes.h.inc"
43 } // namespace fir
44 
45 #define DEBUG_TYPE "vscale-attr"
46 
47 namespace {
48 
49 class VScaleAttrPass : public fir::impl::VScaleAttrBase<VScaleAttrPass> {
50 public:
VScaleAttrPass(const fir::VScaleAttrOptions & options)51   VScaleAttrPass(const fir::VScaleAttrOptions &options) {
52     vscaleRange = options.vscaleRange;
53   }
VScaleAttrPass()54   VScaleAttrPass() {}
55   void runOnOperation() override;
56 };
57 
58 } // namespace
59 
runOnOperation()60 void VScaleAttrPass::runOnOperation() {
61   LLVM_DEBUG(llvm::dbgs() << "=== Begin " DEBUG_TYPE " ===\n");
62   mlir::func::FuncOp func = getOperation();
63 
64   LLVM_DEBUG(llvm::dbgs() << "Func-name:" << func.getSymName() << "\n");
65 
66   auto context = &getContext();
67 
68   auto intTy = mlir::IntegerType::get(context, 32);
69 
70   assert(vscaleRange.first && "VScaleRange minimum should be non-zero");
71 
72   func->setAttr("vscale_range",
73                 mlir::LLVM::VScaleRangeAttr::get(
74                     context, mlir::IntegerAttr::get(intTy, vscaleRange.first),
75                     mlir::IntegerAttr::get(intTy, vscaleRange.second)));
76 
77   LLVM_DEBUG(llvm::dbgs() << "=== End " DEBUG_TYPE " ===\n");
78 }
79