Skip to content

Commit

Permalink
[WebAssembly] Fix EH feature flags when compiling multiple files (llv…
Browse files Browse the repository at this point in the history
…m#124374)

llvm#124042 caused a problem that when invoking `clang` with multiple files,
the static `HasRun` variables were set when processing the first file so
the appropriate feature flags were not added from the second file. This
fixes the problem by making those `HasRun` variables just normal
variables within the enclosing function.
  • Loading branch information
aheejin authored Jan 25, 2025
1 parent 07ed818 commit 4ea44eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 7 additions & 6 deletions clang/lib/Driver/ToolChains/WebAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,15 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
}
}

bool HasBannedIncompatibleOptionsForWasmEHSjLj = false;
bool HasEnabledFeaturesForWasmEHSjLj = false;

// Bans incompatible options for Wasm EH / SjLj. We don't allow using
// different modes for EH and SjLj.
auto BanIncompatibleOptionsForWasmEHSjLj = [&](StringRef CurOption) {
static bool HasRun = false;
if (HasRun)
if (HasBannedIncompatibleOptionsForWasmEHSjLj)
return;
HasBannedIncompatibleOptionsForWasmEHSjLj = true;
if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
options::OPT_mexception_handing, false))
getDriver().Diag(diag::err_drv_argument_not_allowed_with)
Expand All @@ -373,14 +376,13 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
<< CurOption << Option;
}
}
HasRun = true;
};

// Enable necessary features for Wasm EH / SjLj in the backend.
auto EnableFeaturesForWasmEHSjLj = [&]() {
static bool HasRun = false;
if (HasRun)
if (HasEnabledFeaturesForWasmEHSjLj)
return;
HasEnabledFeaturesForWasmEHSjLj = true;
CC1Args.push_back("-target-feature");
CC1Args.push_back("+exception-handling");
// The standardized Wasm EH spec requires multivalue and reference-types.
Expand All @@ -390,7 +392,6 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
CC1Args.push_back("+reference-types");
// Backend needs '-exception-model=wasm' to use Wasm EH instructions
CC1Args.push_back("-exception-model=wasm");
HasRun = true;
};

if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Driver/wasm-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@
// RUN: | FileCheck -check-prefix=WASM_LEGACY_EH_NO_EH %s
// WASM_LEGACY_EH_NO_EH: invalid argument '-wasm-use-legacy-eh' not allowed with '-mno-exception-handling'

// When invoking clang with multiple files in a single command line, target
// feature flags should be equally added to the multiple clang-cc1 command lines
// RUN: %clang -### --target=wasm32-unknown-unknown \
// RUN: --sysroot=/foo %s %s -mllvm -wasm-enable-sjlj 2>&1 \
// RUN: | FileCheck -check-prefix=WASM_SJLJ_MULTI_FILES %s
// WASM_SJLJ_MULTI_FILES: "-cc1" {{.*}} "-target-feature" "+exception-handling" "-target-feature" "+multivalue" "-target-feature" "+reference-types" "-exception-model=wasm"
// WASM_SJLJ_MULTI_FILES: "-cc1" {{.*}} "-target-feature" "+exception-handling" "-target-feature" "+multivalue" "-target-feature" "+reference-types" "-exception-model=wasm"

// RUN: %clang -### %s -fsanitize=address --target=wasm32-unknown-emscripten 2>&1 | FileCheck -check-prefix=CHECK-ASAN-EMSCRIPTEN %s
// CHECK-ASAN-EMSCRIPTEN: "-fsanitize=address"
// CHECK-ASAN-EMSCRIPTEN: "-fsanitize-address-globals-dead-stripping"
Expand Down

0 comments on commit 4ea44eb

Please sign in to comment.