Skip to content

Commit

Permalink
Implement custom constant materializer to fix constant folding
Browse files Browse the repository at this point in the history
  • Loading branch information
lgeiger committed Apr 5, 2022
1 parent 528c819 commit b130e7c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions larq_compute_engine/mlir/ir/lce_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "flatbuffers/flexbuffers.h"
#include "larq_compute_engine/core/bitpacking/bitpack.h"
#include "larq_compute_engine/mlir/transforms/bitpack.h"
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
#include "tensorflow/lite/schema/schema_generated.h"

// Generated dialect defs.
Expand Down Expand Up @@ -76,5 +77,12 @@ void LarqDialect::initialize() {
#include "larq_compute_engine/mlir/ir/lce_ops.cc.inc"
>();
}

Operation* LarqDialect::materializeConstant(OpBuilder& builder, Attribute value,
Type type, Location loc) {
if (arith::ConstantOp::isBuildableWith(value, type))
return builder.create<mlir::arith::ConstantOp>(loc, type, value);
return nullptr;
}
} // namespace lq
} // namespace mlir
2 changes: 2 additions & 0 deletions larq_compute_engine/mlir/ir/lce_ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def LarqDialect : Dialect {
TF graphs to be deployed on Larq Compute Engine.
}];

let hasConstantMaterializer = 1;

let cppNamespace = "::mlir::lq";
}

Expand Down

0 comments on commit b130e7c

Please sign in to comment.