Skip to content

Commit 36747a6

Browse files
HerrCai0907atc-github
authored andcommitted
chore(common): wrapper BinaryenModule with AsModule to support more information from frontend to optimizer (#119)
1 parent 0c0fc3b commit 36747a6

File tree

7 files changed

+84
-56
lines changed

7 files changed

+84
-56
lines changed

frontend/CompilerImpl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,25 +357,25 @@ warpo::frontend::CompilationResult FrontendCompiler::compile(std::vector<std::st
357357
}
358358
}
359359
if (checkDiag(program, config.useColorfulDiagMessage))
360-
return {.m = nullptr, .errorMessage = errorMessage_};
360+
return {.m = {}, .errorMessage = errorMessage_};
361361
parseStat.release();
362362

363363
support::PerfRAII compileStat{support::PerfItemKind::CompilationHIR_Compilation};
364364
m.callExportedFunctionWithName<0>(stackTop, "initializeProgram", program);
365365
int32_t const compiled = m.callExportedFunctionWithName<1>(stackTop, "compile", program)[0].i32;
366366
if (checkDiag(program, config.useColorfulDiagMessage))
367-
return {.m = nullptr, .errorMessage = errorMessage_};
367+
return {.m = {}, .errorMessage = errorMessage_};
368368
wasm::Module *binaryen_module = reinterpret_cast<wasm::Module *>(
369369
m.callExportedFunctionWithName<1>(stackTop, "getBinaryenModuleRef", compiled)[0].i64);
370370
compileStat.release();
371-
return {.m = BinaryenModule{binaryen_module}, .errorMessage = std::nullopt};
371+
return {.m = AsModule{binaryen_module}, .errorMessage = errorMessage_};
372372
} catch (vb::TrapException const &e) {
373373
logger << "Error: " << e.what() << vb::endStatement;
374374
m.printStacktrace(logger);
375375
} catch (std::exception const &e) {
376376
logger << "Error: " << e.what() << vb::endStatement;
377377
}
378-
return {.m = nullptr, .errorMessage = "AS wasm execution failed"};
378+
return {.m = {}, .errorMessage = "AS wasm execution failed"};
379379
}
380380

381381
} // namespace warpo::frontend

include/warpo/common/AsModule.hpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (C) 2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#pragma once
18+
19+
#include <binaryen/src/binaryen-c.h>
20+
#include <cstddef>
21+
#include <utility>
22+
23+
namespace warpo {
24+
25+
class BinaryenModule {
26+
BinaryenModuleRef ref_;
27+
28+
public:
29+
BinaryenModule(BinaryenModule const &) = delete;
30+
BinaryenModule(BinaryenModule &&o) noexcept : ref_(o.ref_) {
31+
if (&o != this)
32+
o.ref_ = nullptr;
33+
}
34+
BinaryenModule &operator=(BinaryenModule const &) = delete;
35+
BinaryenModule &operator=(BinaryenModule &&o) noexcept {
36+
BinaryenModuleRef tmp = ref_;
37+
ref_ = o.ref_;
38+
o.ref_ = ref_;
39+
return *this;
40+
}
41+
42+
BinaryenModule() noexcept : BinaryenModule(nullptr) {}
43+
BinaryenModule(BinaryenModuleRef ref) noexcept : ref_(ref) {}
44+
~BinaryenModule() noexcept {
45+
if (ref_)
46+
BinaryenModuleDispose(ref_);
47+
}
48+
49+
BinaryenModuleRef get() const noexcept { return static_cast<BinaryenModuleRef>(ref_); }
50+
51+
bool operator==(std::nullptr_t) const { return ref_ == nullptr; }
52+
};
53+
54+
class AsModule {
55+
BinaryenModule raw_;
56+
// TODO: more information fields
57+
58+
public:
59+
AsModule() = default;
60+
explicit AsModule(BinaryenModule raw) : raw_(std::move(raw)) {}
61+
BinaryenModuleRef get() const { return raw_.get(); }
62+
bool valid() const { return raw_ != nullptr; }
63+
bool invalid() const { return !valid(); }
64+
};
65+
66+
} // namespace warpo

include/warpo/common/BinaryenRAII.hpp

Lines changed: 0 additions & 37 deletions
This file was deleted.

include/warpo/frontend/Compiler.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <string>
77
#include <vector>
88

9-
#include "warpo/common/BinaryenRAII.hpp"
9+
#include "warpo/common/AsModule.hpp"
1010
#include "warpo/common/Features.hpp"
1111

1212
namespace wasm {
@@ -34,8 +34,8 @@ struct Config {
3434
Config getDefaultConfig();
3535

3636
struct CompilationResult {
37-
BinaryenModule m;
38-
std::optional<std::string> errorMessage;
37+
AsModule m;
38+
std::string errorMessage;
3939
};
4040

4141
CompilationResult compile();

tests/frontend/FrontendTest.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ frontend::CompilationResult compile(nlohmann::json const &configJson, std::files
6161
[[nodiscard]] TestResult runUpdate(nlohmann::json const &configJson, std::filesystem::path const &tsPath,
6262
std::filesystem::path const &expectedOutPath) {
6363
frontend::CompilationResult const ret = compile(configJson, tsPath);
64-
if (ret.m == nullptr) {
64+
if (ret.m.invalid()) {
6565
if (configJson.contains("stderr")) {
6666
return TestResult::Success;
6767
} else {
6868
fmt::println("FAILED '{}': expected to compile successfully\nerror message: {}", tsPath.c_str(),
69-
*ret.errorMessage);
69+
ret.errorMessage);
7070
return TestResult::Failure;
7171
}
7272
}
@@ -80,7 +80,7 @@ frontend::CompilationResult compile(nlohmann::json const &configJson, std::files
8080
[[nodiscard]] TestResult runCompilationErrorCase(nlohmann::json const &configJson,
8181
std::filesystem::path const &tsPath) {
8282
frontend::CompilationResult const ret = compile(configJson, tsPath);
83-
if (ret.m != nullptr) {
83+
if (ret.m.valid()) {
8484
fmt::println("'{}' success to compile but expect failed", tsPath.c_str());
8585
return TestResult::Failure;
8686
}
@@ -94,12 +94,11 @@ frontend::CompilationResult compile(nlohmann::json const &configJson, std::files
9494
}
9595
}
9696

97-
std::string const &actualStderr = *ret.errorMessage;
9897
size_t lastIndex = 0;
9998
bool failed = false;
10099
for (auto const &expectedErrorMessageLineJson : configJson["stderr"].get<nlohmann::json::array_t>()) {
101100
std::string const expectedErrorMessageLine = expectedErrorMessageLineJson.get<std::string>();
102-
size_t index = actualStderr.find(expectedErrorMessageLine, lastIndex);
101+
size_t index = ret.errorMessage.find(expectedErrorMessageLine, lastIndex);
103102
if (index == std::string::npos) {
104103
fmt::println("\tmissing pattern '{}' in stderr.", expectedErrorMessageLine);
105104
failed = true;
@@ -111,7 +110,7 @@ frontend::CompilationResult compile(nlohmann::json const &configJson, std::files
111110
fmt::println("\t========== actual error message==========\n"
112111
"{}"
113112
"\t========== actual error message==========",
114-
actualStderr);
113+
ret.errorMessage);
115114
fmt::println("FAILED '{}': stderr mismatch", tsPath.c_str());
116115
return TestResult::Failure;
117116
}
@@ -121,8 +120,8 @@ frontend::CompilationResult compile(nlohmann::json const &configJson, std::files
121120
[[nodiscard]] TestResult runSnapshotCase(nlohmann::json const &configJson, std::filesystem::path const &tsPath,
122121
std::filesystem::path const &expectedOutPath) {
123122
frontend::CompilationResult const ret = compile(configJson, tsPath);
124-
if (ret.m == nullptr) {
125-
fmt::println("FAILED '{}': expected to compile successfully\nerror message: {}", tsPath.c_str(), *ret.errorMessage);
123+
if (ret.m.invalid()) {
124+
fmt::println("FAILED '{}': expected to compile successfully\nerror message: {}", tsPath.c_str(), ret.errorMessage);
126125
return TestResult::Failure;
127126
}
128127
std::stringstream ss;

tools/asc/AscMain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ void ascMain(int argc, const char *argv[]) {
2727
cli::init(cli::Category::Frontend | cli::Category::Optimization, program, argc, argv);
2828

2929
frontend::CompilationResult const result = frontend::compile();
30-
if (result.m == nullptr) {
30+
if (result.m.invalid()) {
3131
fmt::println("compilation failed");
32-
fmt::println("{}", result.errorMessage.value_or("unknown error"));
32+
fmt::println("{}", result.errorMessage);
3333
throw std::runtime_error("compilation failed");
3434
}
3535
passes::runAndEmit(result.m.get(), outputPath.get());

tools/compiler/CompilerMain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ void compilerMain(int argc, const char *argv[]) {
2424
cli::init(cli::Category::Frontend, program, argc, argv);
2525

2626
frontend::CompilationResult const result = frontend::compile();
27-
if (result.m == nullptr) {
27+
if (result.m.invalid()) {
2828
fmt::println("compilation failed");
29-
fmt::println("{}", result.errorMessage.value_or("unknown error"));
29+
fmt::println("{}", result.errorMessage);
3030
throw std::runtime_error("compilation failed");
3131
}
3232
char *const wasmText = BinaryenModuleAllocateAndWriteText(result.m.get());

0 commit comments

Comments
 (0)