-
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.
Add external debug port during execution
- Loading branch information
1 parent
3f984f7
commit 9afa3e8
Showing
13 changed files
with
390 additions
and
20 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
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 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 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
Oops, something went wrong.