Skip to content

Commit beaaa6d

Browse files
committed
Better set constant arg.
1 parent f82eb4e commit beaaa6d

2 files changed

Lines changed: 72 additions & 8 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <llvm/ADT/ArrayRef.h>
4+
#include <mlir/Dialect/Func/IR/FuncOps.h>
5+
#include <mlir/IR/BuiltinAttributeInterfaces.h>
6+
#include <mlir/Support/LLVM.h>
7+
8+
namespace graphalg {
9+
10+
/**
11+
* Inlines constant values for function parameters.
12+
*
13+
* The function parameters to set to constants must be scalar.
14+
*
15+
* Constant parameters are NOT removed from the function signature,
16+
* but the corresponding block arguments will have zero uses.
17+
*
18+
* @param op the function for which to inline argument values.
19+
* @param values for each parameter, the constant value to inline, or null if
20+
* the argument is not constant.
21+
*/
22+
mlir::LogicalResult
23+
setConstantArguments(mlir::func::FuncOp op,
24+
llvm::ArrayRef<mlir::TypedAttr> values);
25+
26+
} // namespace graphalg

compiler/src/graphalg/GraphAlgSetConstArg.cpp

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
#include <llvm/ADT/SmallVector.h>
2+
#include <llvm/Support/Casting.h>
3+
#include <mlir/IR/BuiltinAttributeInterfaces.h>
14
#include <mlir/IR/PatternMatch.h>
25
#include <mlir/Pass/Pass.h>
6+
#include <mlir/Support/LLVM.h>
37
#include <mlir/Transforms/DialectConversion.h>
48

5-
#include "graphalg/GraphAlgOps.h"
69
#include "graphalg/GraphAlgPasses.h"
7-
#include "graphalg/GraphAlgTypes.h"
8-
#include "graphalg/SemiringTypes.h"
10+
#include "graphalg/GraphAlgSetConstArg.h"
911

1012
namespace graphalg {
1113

@@ -59,12 +61,48 @@ void GraphAlgSetConstArg::runOnOperation() {
5961
return signalPassFailure();
6062
}
6163

62-
mlir::IRRewriter rewriter(func);
64+
llvm::SmallVector<mlir::TypedAttr> values(numArgs);
65+
values[argumentNumber] =
66+
mlir::IntegerAttr::get(mlir::IntegerType::get(&getContext(), 64), value);
67+
if (mlir::failed(setConstantArguments(func, values))) {
68+
signalPassFailure();
69+
}
70+
}
71+
72+
mlir::LogicalResult
73+
setConstantArguments(mlir::func::FuncOp op,
74+
llvm::ArrayRef<mlir::TypedAttr> values) {
75+
auto &body = op.getBody().front();
76+
auto numArgs = body.getNumArguments();
77+
if (values.size() != numArgs) {
78+
return op.emitOpError("expected a function with ")
79+
<< values.size() << " parameters, but only has " << numArgs;
80+
}
81+
82+
mlir::IRRewriter rewriter(op);
6383
rewriter.setInsertionPointToStart(&body);
64-
auto constOp = rewriter.create<ConstantMatrixOp>(
65-
func.getLoc(), MatrixType::scalarOf(SemiringTypes::forInt(&getContext())),
66-
rewriter.getI64IntegerAttr(value));
67-
rewriter.replaceAllUsesWith(body.getArgument(argumentNumber), constOp);
84+
85+
for (auto i : llvm::seq(numArgs)) {
86+
auto val = values[i];
87+
if (!val) {
88+
// Not constant
89+
continue;
90+
}
91+
92+
auto arg = body.getArgument(i);
93+
auto type = llvm::dyn_cast<MatrixType>(arg.getType());
94+
if (!type || !type.isScalar()) {
95+
return op.emitOpError("argument ") << i << " is not a scalar matrix";
96+
} else if (type.getSemiring() != val.getType()) {
97+
return op.emitOpError("cannot inline value of type ")
98+
<< val.getType() << " into argument " << i << " of type " << type;
99+
}
100+
101+
auto constOp = rewriter.create<ConstantMatrixOp>(op.getLoc(), type, val);
102+
rewriter.replaceAllUsesWith(body.getArgument(i), constOp);
103+
}
104+
105+
return mlir::success();
68106
}
69107

70108
} // namespace graphalg

0 commit comments

Comments
 (0)