-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds pass to convert secret annotated arguments to secret types
and wrap function body in secret.generic Signed-off-by: Asra <asraa@google.com>
- Loading branch information
Showing
8 changed files
with
140 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <iostream> | ||
|
||
#include "include/Dialect/Secret/IR/SecretDialect.h" | ||
#include "include/Dialect/Secret/IR/SecretOps.h" | ||
#include "include/Dialect/Secret/IR/SecretTypes.h" | ||
#include "include/Transforms/Secretize/Passes.h" | ||
#include "mlir/include/mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/IRMapping.h" // from @llvm-project | ||
#include "mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project | ||
#include "mlir/include/mlir/Transforms/Passes.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace heir { | ||
|
||
#define GEN_PASS_DEF_WRAPGENERIC | ||
#include "include/Transforms/Secretize/Passes.h.inc" | ||
|
||
struct WrapWithGeneric : public OpRewritePattern<func::FuncOp> { | ||
WrapWithGeneric(mlir::MLIRContext *context) | ||
: mlir::OpRewritePattern<func::FuncOp>(context) {} | ||
|
||
LogicalResult matchAndRewrite(func::FuncOp op, | ||
PatternRewriter &rewriter) const override { | ||
bool hasSecrets = false; | ||
|
||
SmallVector<Type, 4> newInputs; | ||
for (unsigned i = 0; i < op.getNumArguments(); i++) { | ||
if (op.getArgAttr(i, secret::SecretDialect::kArgSecretAttrName) != | ||
nullptr) { | ||
hasSecrets = true; | ||
op.removeArgAttr(i, secret::SecretDialect::kArgSecretAttrName); | ||
|
||
auto newTy = secret::SecretType::get(op.getArgument(i).getType()); | ||
op.getArgument(i).setType(newTy); // Updates the block argument type. | ||
newInputs.push_back(newTy); | ||
} | ||
} | ||
|
||
if (!hasSecrets) { | ||
// Match failure, no secret inputs. | ||
return failure(); | ||
} | ||
|
||
llvm::SmallVector<Type, 4> newOutputs; | ||
for (unsigned i = 0; i < op.getNumResults(); i++) { | ||
newOutputs.push_back(secret::SecretType::get(op.getResultTypes()[i])); | ||
} | ||
|
||
op.setFunctionType( | ||
FunctionType::get(getContext(), {newInputs}, {newOutputs})); | ||
|
||
// Create a secret.generic op and pull the original function block in. | ||
Block &opEntryBlock = op.getRegion().front(); | ||
rewriter.setInsertionPointToStart(&opEntryBlock); | ||
auto newGeneric = rewriter.create<secret::GenericOp>( | ||
op.getLoc(), op.getArguments(), newOutputs, | ||
[&](OpBuilder &b, Location loc, ValueRange blockArguments) { | ||
// Map the input values to the block arguments. | ||
IRMapping mp; | ||
for (unsigned i = 0; i < blockArguments.size(); ++i) { | ||
mp.map(opEntryBlock.getArgument(i), blockArguments[i]); | ||
} | ||
for (auto &entryOp : opEntryBlock.getOperations()) { | ||
b.clone(entryOp, mp); | ||
} | ||
auto *returnOp = b.getBlock()->getTerminator(); | ||
b.create<secret::YieldOp>(loc, returnOp->getOperands()); | ||
returnOp->erase(); | ||
}); | ||
|
||
rewriter.replaceOp( | ||
opEntryBlock.getTerminator(), | ||
rewriter.create<func::ReturnOp>(op.getLoc(), newGeneric.getResults())); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
struct WrapGeneric : impl::WrapGenericBase<WrapGeneric> { | ||
using WrapGenericBase::WrapGenericBase; | ||
|
||
void runOnOperation() override { | ||
MLIRContext *context = &getContext(); | ||
|
||
mlir::RewritePatternSet patterns(context); | ||
patterns.add<WrapWithGeneric>(context); | ||
|
||
(void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); | ||
} | ||
}; | ||
|
||
} // namespace heir | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// RUN: heir-opt --wrap-generic %s | FileCheck %s | ||
|
||
// CHECK: module | ||
module { | ||
func.func @main(%value: i32 {secret.secret}, %cond: i1 {secret.secret}) -> (i32) { | ||
%c0 = arith.constant 0 : i32 | ||
%c1 = arith.constant 1 : i32 | ||
%c7 = arith.constant 7 : i32 | ||
%0 = arith.muli %value, %c7 : i32 | ||
%1 = arith.addi %0, %c1 : i32 | ||
%2 = arith.muli %1, %1 : i32 | ||
%3 = arith.select %cond, %2, %c0 : i32 | ||
func.return %3 : i32 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters