-
Notifications
You must be signed in to change notification settings - Fork 109
Add external debug port during execution #1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,118 @@ | ||
| #include "lib/Dialect/LWE/Transforms/AddDebugPort.h" | ||
|
|
||
| #include "lib/Dialect/LWE/IR/LWEOps.h" | ||
| #include "lib/Dialect/LWE/IR/LWETypes.h" | ||
| #include "llvm/include/llvm/ADT/TypeSwitch.h" // from @llvm-project | ||
| #include "mlir/include/mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project | ||
| #include "mlir/include/mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project | ||
| #include "mlir/include/mlir/Interfaces/FunctionInterfaces.h" // from @llvm-project | ||
| #include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project | ||
|
|
||
| namespace mlir { | ||
| namespace heir { | ||
| namespace lwe { | ||
|
|
||
| #define GEN_PASS_DEF_ADDDEBUGPORT | ||
| #include "lib/Dialect/LWE/Transforms/Passes.h.inc" | ||
|
|
||
| FailureOr<Type> getPrivateKeyType(func::FuncOp op) { | ||
| const auto *type = llvm::find_if(op.getArgumentTypes(), [](Type type) { | ||
| return mlir::isa<NewLWECiphertextType>(type); | ||
| }); | ||
|
|
||
| if (type == op.getArgumentTypes().end()) { | ||
| return op.emitError( | ||
| "Function does not have an argument of LWECiphertextType"); | ||
| } | ||
|
|
||
| auto lweCiphertextType = cast<NewLWECiphertextType>(*type); | ||
|
|
||
| auto lwePrivateKeyType = NewLWESecretKeyType::get( | ||
| op.getContext(), lweCiphertextType.getKey(), | ||
| lweCiphertextType.getCiphertextSpace().getRing()); | ||
| return lwePrivateKeyType; | ||
| } | ||
|
|
||
| func::FuncOp getOrCreateExternalDebugFunc( | ||
| ModuleOp module, Type lwePrivateKeyType, | ||
| NewLWECiphertextType lweCiphertextType, | ||
| const DenseMap<Type, int> &typeToInt) { | ||
| std::string funcName = | ||
| "__heir_debug_" + std::to_string(typeToInt.at(lweCiphertextType)); | ||
|
|
||
| auto *context = module.getContext(); | ||
| auto lookup = module.lookupSymbol<func::FuncOp>(funcName); | ||
| if (lookup) return lookup; | ||
|
|
||
| auto debugFuncType = | ||
| FunctionType::get(context, {lwePrivateKeyType, lweCiphertextType}, {}); | ||
|
|
||
| ImplicitLocOpBuilder b = | ||
| ImplicitLocOpBuilder::atBlockBegin(module.getLoc(), module.getBody()); | ||
| auto funcOp = b.create<func::FuncOp>(funcName, debugFuncType); | ||
| // required for external func call | ||
| funcOp.setPrivate(); | ||
| return funcOp; | ||
| } | ||
|
|
||
| LogicalResult insertExternalCall(func::FuncOp op, Type lwePrivateKeyType) { | ||
| auto module = op->getParentOfType<ModuleOp>(); | ||
|
|
||
| // map ciphertext type to unique int | ||
| DenseMap<Type, int> typeToInt; | ||
|
|
||
| // implicit assumption the first argument is private key | ||
| auto privateKey = op.getArgument(0); | ||
|
|
||
| ImplicitLocOpBuilder b = | ||
| ImplicitLocOpBuilder::atBlockBegin(module.getLoc(), module.getBody()); | ||
| op.walk([&](Operation *op) { | ||
| b.setInsertionPointAfter(op); | ||
| for (Value result : op->getResults()) { | ||
| Type resultType = result.getType(); | ||
| if (auto lweCiphertextType = dyn_cast<NewLWECiphertextType>(resultType)) { | ||
| // update typeToInt | ||
| if (!typeToInt.count(resultType)) { | ||
| typeToInt[resultType] = typeToInt.size(); | ||
| } | ||
| b.create<func::CallOp>( | ||
| getOrCreateExternalDebugFunc(module, lwePrivateKeyType, | ||
| lweCiphertextType, typeToInt), | ||
| ArrayRef<Value>{privateKey, result}); | ||
| } | ||
| } | ||
| return WalkResult::advance(); | ||
| }); | ||
| return success(); | ||
| } | ||
|
|
||
| LogicalResult convertFunc(func::FuncOp op) { | ||
| auto type = getPrivateKeyType(op); | ||
| if (failed(type)) return failure(); | ||
| auto lwePrivateKeyType = type.value(); | ||
|
|
||
| op.insertArgument(0, lwePrivateKeyType, nullptr, op.getLoc()); | ||
| if (failed(insertExternalCall(op, lwePrivateKeyType))) { | ||
| return failure(); | ||
| } | ||
| return success(); | ||
| } | ||
|
|
||
| struct AddDebugPort : impl::AddDebugPortBase<AddDebugPort> { | ||
| using AddDebugPortBase::AddDebugPortBase; | ||
|
|
||
| void runOnOperation() override { | ||
| auto result = | ||
| getOperation()->walk<WalkOrder::PreOrder>([&](func::FuncOp op) { | ||
| if (op.getSymName() == entryFunction && failed(convertFunc(op))) { | ||
| op->emitError("Failed to add client interface for func"); | ||
| return WalkResult::interrupt(); | ||
| } | ||
| return WalkResult::advance(); | ||
| }); | ||
| if (result.wasInterrupted()) signalPassFailure(); | ||
| } | ||
| }; | ||
| } // namespace lwe | ||
| } // namespace heir | ||
| } // namespace mlir |
This file contains hidden or 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,17 @@ | ||
| #ifndef LIB_DIALECT_LWE_TRANSFORMS_ADDDEBUGPORT_H_ | ||
| #define LIB_DIALECT_LWE_TRANSFORMS_ADDDEBUGPORT_H_ | ||
|
|
||
| #include "mlir/include/mlir/Pass/Pass.h" // from @llvm-project | ||
|
|
||
| namespace mlir { | ||
| namespace heir { | ||
| namespace lwe { | ||
|
|
||
| #define GEN_PASS_DECL_ADDDEBUGPORT | ||
| #include "lib/Dialect/LWE/Transforms/Passes.h.inc" | ||
|
|
||
| } // namespace lwe | ||
| } // namespace heir | ||
| } // namespace mlir | ||
|
|
||
| #endif // LIB_DIALECT_LWE_TRANSFORMS_ADDDEBUGPORT_H_ |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.