1//===- TestIntegerDivisibilityAnalysis.cpp - Test int divisibility --------===//
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#include "mlir/Analysis/DataFlow/ConstantPropagationAnalysis.h"
10#include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h"
11#include "mlir/Analysis/DataFlow/IntegerDivisibilityAnalysis.h"
12#include "mlir/Analysis/DataFlowFramework.h"
13#include "mlir/Dialect/Affine/IR/AffineDialect.h"
14#include "mlir/Dialect/Arith/IR/Arith.h"
15#include "mlir/IR/BuiltinAttributes.h"
16#include "mlir/IR/DialectRegistry.h"
17#include "mlir/IR/Operation.h"
18#include "mlir/Pass/Pass.h"
19#include "mlir/Pass/PassRegistry.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/Support/raw_ostream.h"
22
23using namespace mlir;
24using namespace mlir::dataflow;
25
26namespace {
27struct TestIntegerDivisibilityAnalysisPass
28 : public PassWrapper<TestIntegerDivisibilityAnalysisPass, OperationPass<>> {
29 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(
30 TestIntegerDivisibilityAnalysisPass)
31
32 StringRef getArgument() const override {
33 return "test-int-divisibility-analysis";
34 }
35 StringRef getDescription() const override {
36 return "Test integer divisibility analysis by annotating "
37 "'test.int_divisibility' ops with the divisibility of their "
38 "operand.";
39 }
40
41 void getDependentDialects(DialectRegistry &registry) const override {
42 registry.insert<arith::ArithDialect, affine::AffineDialect>();
43 }
44
45 void runOnOperation() override {
46 Operation *rootOp = getOperation();
47 MLIRContext *context = &getContext();
48
49 // The pass is rooted on `test.int_divisibility` ops, which are expected
50 // to have a single operand for which to annotate divisibility information.
51 SmallVector<std::pair<Operation *, Value>> queryOps;
52 rootOp->walk(callback: [&](Operation *op) {
53 if (op->getName().getStringRef() == "test.int_divisibility" &&
54 op->getNumOperands() == 1)
55 queryOps.emplace_back(Args&: op, Args: op->getOperand(idx: 0));
56 });
57
58 DataFlowSolver solver;
59 // DeadCodeAnalysis is the base analysis that allows the solver to traverse
60 // control flow. It is required by IntegerDivisibilityAnalysis.
61 solver.load<DeadCodeAnalysis>();
62 // SparseConstantPropagation allows the solver to call
63 // visitNonControlFlowArguments and analyze arguments like loop induction
64 // variables.
65 solver.load<SparseConstantPropagation>();
66 solver.load<IntegerDivisibilityAnalysis>();
67 if (failed(Result: solver.initializeAndRun(top: rootOp)))
68 return signalPassFailure();
69
70 for (auto &[op, value] : queryOps) {
71 const auto *lattice =
72 solver.lookupState<IntegerDivisibilityLattice>(anchor: value);
73 if (!lattice || lattice->getValue().isUninitialized()) {
74 op->setDiscardableAttr(name: "divisibility",
75 value: StringAttr::get(context, bytes: "uninitialized"));
76 continue;
77 }
78
79 // Format for the divisibility information is "udiv = X, sdiv = Y".
80 const auto &div = lattice->getValue().getValue();
81 std::string result;
82 llvm::raw_string_ostream os(result);
83 os << "udiv = " << div.udiv() << ", sdiv = " << div.sdiv();
84 op->setDiscardableAttr(name: "divisibility", value: StringAttr::get(context, bytes: result));
85 }
86 }
87};
88} // end anonymous namespace
89
90namespace mlir::test {
91void registerTestIntegerDivisibilityAnalysisPass() {
92 PassRegistration<TestIntegerDivisibilityAnalysisPass>();
93}
94} // end namespace mlir::test
95