From ad6a9000cc97dd018bffe961e8483410e77011e4 Mon Sep 17 00:00:00 2001 From: luis Date: Thu, 30 Nov 2023 17:17:32 -0800 Subject: [PATCH] Do not throw error when executing empty StableHLO modules (#1864) This PR addresses a bug introduced by PR #1797 where as a result of the new interpreter API, running empty StableHLO modules would result in an error `Requested main function not found.` whereas previously, the `stablehlo-translate` utility would not throw any error. (Found by @ghpvnist in #1841). --- stablehlo/reference/Api.cpp | 4 ++++ stablehlo/tests/interpret_probe.mlir | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/stablehlo/reference/Api.cpp b/stablehlo/reference/Api.cpp index d39a46c8c04..a94f0c44cce 100644 --- a/stablehlo/reference/Api.cpp +++ b/stablehlo/reference/Api.cpp @@ -14,6 +14,7 @@ limitations under the License. ==============================================================================*/ #include "stablehlo/reference/Api.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" @@ -106,6 +107,9 @@ class DefaultInterpreterFallback : public InterpreterFallback { llvm::ErrorOr> evalModule( ModuleOp module, ArrayRef inputs, const InterpreterConfiguration &config) { + if (module.getOps().empty()) + return SmallVector(); + auto mainFunc = getMainFunction(module, config.mainFunction); if (!mainFunc) llvm::report_fatal_error("Requested main function not found."); diff --git a/stablehlo/tests/interpret_probe.mlir b/stablehlo/tests/interpret_probe.mlir index c903dfef1fa..bc1d8178644 100644 --- a/stablehlo/tests/interpret_probe.mlir +++ b/stablehlo/tests/interpret_probe.mlir @@ -1,5 +1,9 @@ // RUN: stablehlo-translate --interpret --probe-output-dir=%T -split-input-file %s +// Test an empty module + +// ----- + func.func @probe_i1() { %0 = stablehlo.constant dense<[[0], [0], [0]]> : tensor<3x1xi1> %1 = stablehlo.constant dense<[[1], [1], [1]]> : tensor<3x1xi1>