-
Notifications
You must be signed in to change notification settings - Fork 319
/
KrnlHelper.hpp
104 lines (80 loc) · 3.52 KB
/
KrnlHelper.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* SPDX-License-Identifier: Apache-2.0
*/
//====---------------- KrnlHelper.hpp - Krnl Dialect Helper----------------===//
//
// Copyright 2019-2024 The IBM Research Authors.
//
// =============================================================================
//
// This file implements helper methods to build Krnl Dialect ops.
//
//===----------------------------------------------------------------------===//
#ifndef ONNX_MLIR_KRNL_HELPER_H
#define ONNX_MLIR_KRNL_HELPER_H
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/OpImplementation.h"
#include "src/Dialect/Mlir/IndexExpr.hpp"
namespace onnx_mlir {
namespace krnl {
// Adapted from:
// https://github.com/tensorflow/mlir/blob/6a150d70c7e06fb37cddd7188fa48cde9a90fe59/lib/Dialect/StandardOps/Ops.cpp#L197
// Main difference is that it advances the iterator `begin` as it consumes
// dimension and symbol operands.
void printDimAndSymbolList(mlir::Operation::operand_iterator &begin,
unsigned numDims, unsigned numSymbols, mlir::OpAsmPrinter &p);
// Adapted from:
// https://github.com/tensorflow/mlir/blob/5cb42c914fed14cebbbe5c170b4e2784d2628304/lib/Dialect/AffineOps/AffineOps.cpp#L1272
// Main difference is that it advances the iterator `boundOperandsBeg` as it
// prints bound.
void printBound(mlir::AffineMapAttr boundMap,
mlir::Operation::operand_iterator &boundOperandsBeg, const char *prefix,
mlir::OpAsmPrinter &p);
struct KrnlIterateOperandPack {
KrnlIterateOperandPack(mlir::Builder &builder,
llvm::ArrayRef<mlir::Value> inputLoops,
llvm::ArrayRef<mlir::Value> optimizedLoops)
: inputLoops(inputLoops), optimizedLoops(optimizedLoops),
builder(builder) {
operands.insert(
operands.end(), optimizedLoops.begin(), optimizedLoops.end());
}
// Create a pack with optimizedLoops = inputLoops (ie., no optimization).
KrnlIterateOperandPack(
mlir::Builder &builder, llvm::ArrayRef<mlir::Value> inputLoops)
: inputLoops(inputLoops), optimizedLoops(inputLoops), builder(builder) {
operands.insert(operands.end(), inputLoops.begin(), inputLoops.end());
}
void pushConstantBound(int64_t bound);
void pushOperandBound(mlir::Value operand);
void pushAffineMapBound(
mlir::AffineMap map, mlir::ArrayRef<mlir::Value> operands);
// When used in a lower bound, set isLb to true, when used in an upper bound,
// set isLb to false.
void pushIndexExprBound(IndexExpr expr, bool isLb);
void pushIndexExprsBound(llvm::SmallVectorImpl<IndexExpr> &exprVector);
llvm::SmallVector<mlir::Value, 8> getOperands() const { return operands; }
mlir::ArrayAttr getAttributes() const {
return builder.getArrayAttr(boundMaps);
}
size_t getNumOptimizedLoops() const { return optimizedLoops.size(); }
size_t getNumInputLoops() const { return inputLoops.size(); }
private:
llvm::SmallVector<mlir::Value, 8> operands;
llvm::SmallVector<mlir::Attribute, 8> boundMaps;
llvm::ArrayRef<mlir::Value> inputLoops, optimizedLoops;
mlir::Builder &builder;
};
mlir::DenseElementsAttr getDenseElementAttributeFromKrnlValue(
mlir::Value value);
//====---------------- Support for simple transpose ----------------------===//
void generateIndexMap(
llvm::SmallVectorImpl<int64_t> &map, int64_t size, bool transposeInner2);
//====---------------- Common helper functions --------------------------===//
/// Check whether a value is produced by a dense KrnlGlobalOp.
bool isKrnlGlobalConstant(mlir::Value result);
} // namespace krnl
} // namespace onnx_mlir
#endif