@@ -344,44 +344,53 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
344344 }
345345 }
346346
347- if (DriverArgs.getLastArg (options::OPT_fwasm_exceptions)) {
348- // '-fwasm-exceptions' is not compatible with '-mno-exception-handling'
347+ // Bans incompatible options for Wasm EH / SjLj. We don't allow using
348+ // different modes for EH and SjLj.
349+ auto BanIncompatibleOptionsForWasmEHSjLj = [&](StringRef CurOption) {
349350 if (DriverArgs.hasFlag (options::OPT_mno_exception_handing,
350351 options::OPT_mexception_handing, false ))
351352 getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
352- << " -fwasm-exceptions"
353- << " -mno-exception-handling" ;
354- // '-fwasm-exceptions' is not compatible with
355- // '-mllvm -enable-emscripten-cxx-exceptions'
356- for (const Arg *A : DriverArgs.filtered (options::OPT_mllvm)) {
357- if (StringRef (A->getValue (0 )) == " -enable-emscripten-cxx-exceptions" )
358- getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
359- << " -fwasm-exceptions"
360- << " -mllvm -enable-emscripten-cxx-exceptions" ;
361- }
362- // '-fwasm-exceptions' implies exception-handling feature
363- CC1Args.push_back (" -target-feature" );
364- CC1Args.push_back (" +exception-handling" );
365- // Backend needs -wasm-enable-eh to enable Wasm EH
366- CC1Args.push_back (" -mllvm" );
367- CC1Args.push_back (" -wasm-enable-eh" );
368-
369- // New Wasm EH spec (adopted in Oct 2023) requires multivalue and
370- // reference-types.
353+ << CurOption << " -mno-exception-handling" ;
354+ // The standardized Wasm EH spec requires multivalue and reference-types.
371355 if (DriverArgs.hasFlag (options::OPT_mno_multivalue,
372- options::OPT_mmultivalue, false )) {
356+ options::OPT_mmultivalue, false ))
373357 getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
374- << " -fwasm-exceptions" << " -mno-multivalue" ;
375- }
358+ << CurOption << " -mno-multivalue" ;
376359 if (DriverArgs.hasFlag (options::OPT_mno_reference_types,
377- options::OPT_mreference_types, false )) {
360+ options::OPT_mreference_types, false ))
378361 getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
379- << " -fwasm-exceptions" << " -mno-reference-types" ;
362+ << CurOption << " -mno-reference-types" ;
363+
364+ for (const Arg *A : DriverArgs.filtered (options::OPT_mllvm)) {
365+ for (const auto *Option :
366+ {" -enable-emscripten-cxx-exceptions" , " -enable-emscripten-sjlj" ,
367+ " -emscripten-cxx-exceptions-allowed" }) {
368+ if (StringRef (A->getValue (0 )) == Option)
369+ getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
370+ << CurOption << Option;
371+ }
380372 }
373+ };
374+
375+ // Enable necessary features for Wasm EH / SjLj in the backend.
376+ auto EnableFeaturesForWasmEHSjLj = [&]() {
377+ CC1Args.push_back (" -target-feature" );
378+ CC1Args.push_back (" +exception-handling" );
379+ // The standardized Wasm EH spec requires multivalue and reference-types.
381380 CC1Args.push_back (" -target-feature" );
382381 CC1Args.push_back (" +multivalue" );
383382 CC1Args.push_back (" -target-feature" );
384383 CC1Args.push_back (" +reference-types" );
384+ // Backend needs '-exception-model=wasm' to use Wasm EH instructions
385+ CC1Args.push_back (" -exception-model=wasm" );
386+ };
387+
388+ if (DriverArgs.getLastArg (options::OPT_fwasm_exceptions)) {
389+ BanIncompatibleOptionsForWasmEHSjLj (" -fwasm-exceptions" );
390+ EnableFeaturesForWasmEHSjLj ();
391+ // Backend needs -wasm-enable-eh to enable Wasm EH
392+ CC1Args.push_back (" -mllvm" );
393+ CC1Args.push_back (" -wasm-enable-eh" );
385394 }
386395
387396 for (const Arg *A : DriverArgs.filtered (options::OPT_mllvm)) {
@@ -413,53 +422,11 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
413422 }
414423 }
415424
416- if (Opt.starts_with (" -wasm-enable-sjlj" )) {
417- // '-mllvm -wasm-enable-sjlj' is not compatible with
418- // '-mno-exception-handling'
419- if (DriverArgs.hasFlag (options::OPT_mno_exception_handing,
420- options::OPT_mexception_handing, false ))
421- getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
422- << " -mllvm -wasm-enable-sjlj"
423- << " -mno-exception-handling" ;
424- // '-mllvm -wasm-enable-sjlj' is not compatible with
425- // '-mllvm -enable-emscripten-cxx-exceptions'
426- // because we don't allow Emscripten EH + Wasm SjLj
427- for (const Arg *A : DriverArgs.filtered (options::OPT_mllvm)) {
428- if (StringRef (A->getValue (0 )) == " -enable-emscripten-cxx-exceptions" )
429- getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
430- << " -mllvm -wasm-enable-sjlj"
431- << " -mllvm -enable-emscripten-cxx-exceptions" ;
425+ for (const auto *Option : {" -wasm-enable-eh" , " -wasm-enable-sjlj" }) {
426+ if (Opt.starts_with (Option)) {
427+ BanIncompatibleOptionsForWasmEHSjLj (Option);
428+ EnableFeaturesForWasmEHSjLj ();
432429 }
433- // '-mllvm -wasm-enable-sjlj' is not compatible with
434- // '-mllvm -enable-emscripten-sjlj'
435- for (const Arg *A : DriverArgs.filtered (options::OPT_mllvm)) {
436- if (StringRef (A->getValue (0 )) == " -enable-emscripten-sjlj" )
437- getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
438- << " -mllvm -wasm-enable-sjlj"
439- << " -mllvm -enable-emscripten-sjlj" ;
440- }
441- // '-mllvm -wasm-enable-sjlj' implies exception-handling feature
442- CC1Args.push_back (" -target-feature" );
443- CC1Args.push_back (" +exception-handling" );
444- // Backend needs '-exception-model=wasm' to use Wasm EH instructions
445- CC1Args.push_back (" -exception-model=wasm" );
446-
447- // New Wasm EH spec (adopted in Oct 2023) requires multivalue and
448- // reference-types.
449- if (DriverArgs.hasFlag (options::OPT_mno_multivalue,
450- options::OPT_mmultivalue, false )) {
451- getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
452- << " -mllvm -wasm-enable-sjlj" << " -mno-multivalue" ;
453- }
454- if (DriverArgs.hasFlag (options::OPT_mno_reference_types,
455- options::OPT_mreference_types, false )) {
456- getDriver ().Diag (diag::err_drv_argument_not_allowed_with)
457- << " -mllvm -wasm-enable-sjlj" << " -mno-reference-types" ;
458- }
459- CC1Args.push_back (" -target-feature" );
460- CC1Args.push_back (" +multivalue" );
461- CC1Args.push_back (" -target-feature" );
462- CC1Args.push_back (" +reference-types" );
463430 }
464431 }
465432}
0 commit comments