|
| 1 | +#include <llvm/ADT/SmallVector.h> |
| 2 | +#include <llvm/Support/Casting.h> |
| 3 | +#include <mlir/IR/BuiltinAttributeInterfaces.h> |
1 | 4 | #include <mlir/IR/PatternMatch.h> |
2 | 5 | #include <mlir/Pass/Pass.h> |
| 6 | +#include <mlir/Support/LLVM.h> |
3 | 7 | #include <mlir/Transforms/DialectConversion.h> |
4 | 8 |
|
5 | | -#include "graphalg/GraphAlgOps.h" |
6 | 9 | #include "graphalg/GraphAlgPasses.h" |
7 | | -#include "graphalg/GraphAlgTypes.h" |
8 | | -#include "graphalg/SemiringTypes.h" |
| 10 | +#include "graphalg/GraphAlgSetConstArg.h" |
9 | 11 |
|
10 | 12 | namespace graphalg { |
11 | 13 |
|
@@ -59,12 +61,48 @@ void GraphAlgSetConstArg::runOnOperation() { |
59 | 61 | return signalPassFailure(); |
60 | 62 | } |
61 | 63 |
|
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); |
63 | 83 | 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(); |
68 | 106 | } |
69 | 107 |
|
70 | 108 | } // namespace graphalg |
0 commit comments