anutosh491 wrote:

Hey @vgvassilev , 

There is something to discuss here (that I'm struggling to figure out since the 
past couple of days)

As we had discussed, the above diff should be enough for us to process mllvm 
args for clang-repl right ?

Hence i was passing these args to the Compiler Builder in my toy project, 
expecting exception handling to work right off the bat
```
  clang::IncrementalCompilerBuilder CB;
  CB.SetCompilerArgs({
      "-v",
      "-xc++",
      "-std=c++23",
      "-mllvm", "-enable-emscripten-cxx-exceptions",
      "-mllvm", "-enable-emscripten-sjlj"
  });
  ```
  
  But what I notice is that ... For `try { throw 1; } catch (...) { 0; }`
  
  Case 1: The diff in the PR (that should be sufficient) -> the LLVM IR 
generated is incorrect (**& hence the wasm module generated is incorrect too**)
  ```
void CompilerInstance::parseLLVMArgs() {
  if (!getFrontendOpts().LLVMArgs.empty()) {
    unsigned NumArgs = getFrontendOpts().LLVMArgs.size();
    auto Args = std::make_unique<const char*[]>(NumArgs + 2);
    Args[0] = "clang (LLVM option parsing)";
    for (unsigned i = 0; i != NumArgs; ++i)
      Args[i + 1] = getFrontendOpts().LLVMArgs[i].c_str();
    Args[NumArgs + 1] = nullptr;
    llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get(), /*Overview=*/"",
                                      /*Errs=*/nullptr,
                                      /*VFS=*/&getVirtualFileSystem());
  }
}
  
  ```
  LLVM IR
  ```
  ; ModuleID = 'incr_module_1'
source_filename = "incr_module_1"
target datalayout = 
"e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-emscripten"
@_ZTIi = external constant ptr
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr 
} { i32 65535, ptr @_GLOBAL__sub_I_incr_module_1, ptr null }]
define internal void @__stmts__0() #0 personality ptr @__gxx_personality_v0 {
  %1 = alloca ptr, align 4
  %2 = alloca i32, align 4
  %3 = call ptr @__cxa_allocate_exception(i32 4) #2
  store i32 1, ptr %3, align 16
  call void @__cxa_throw(ptr %3, ptr @_ZTIi, ptr null) #3
  br label %4
4:                                                ; preds = %0
  unreachable
}
declare ptr @__cxa_allocate_exception(i32) #0
declare void @__cxa_throw(ptr, ptr, ptr) #0
declare i32 @__gxx_personality_v0(...) #0
declare ptr @__cxa_begin_catch(ptr) #0
declare void @__cxa_end_catch() #0
; Function Attrs: noinline
define internal void @_GLOBAL__sub_I_incr_module_1() #1 {
  call void @__stmts__0()
  ret void
}
attributes #0 = { 
"target-features"="-atomics,+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,-exception-handling,-extended-const,-fp16,-gc,-multimemory,+multivalue,+mutable-globals,+nontrapping-fptoint,+reference-types,-relaxed-simd,+sign-ext,-simd128,-tail-call,-wide-arithmetic"
 }
attributes #1 = { noinline "no-trapping-math"="true" 
"stack-protector-buffer-size"="8" 
"target-features"="-atomics,+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,-exception-handling,-extended-const,-fp16,-gc,-multimemory,+multivalue,+mutable-globals,+nontrapping-fptoint,+reference-types,-relaxed-simd,+sign-ext,-simd128,-tail-call,-wide-arithmetic"
 }
attributes #2 = { nounwind }
attributes #3 = { noreturn }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!llvm.ident = !{!9}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 1, !"wasm-feature-bulk-memory", i32 43}
!2 = !{i32 1, !"wasm-feature-bulk-memory-opt", i32 43}
!3 = !{i32 1, !"wasm-feature-call-indirect-overlong", i32 43}
!4 = !{i32 1, !"wasm-feature-multivalue", i32 43}
!5 = !{i32 1, !"wasm-feature-mutable-globals", i32 43}
!6 = !{i32 1, !"wasm-feature-nontrapping-fptoint", i32 43}
!7 = !{i32 1, !"wasm-feature-reference-types", i32 43}
!8 = !{i32 1, !"wasm-feature-sign-ext", i32 43}
!9 = !{!"clang version 23.0.0git ([email protected]:anutosh491/llvm-project.git 
d023577ef98490a097108eacaecd2dd114fd7974)"}
  ```
  
<img width="703" height="335" alt="image" 
src="https://github.com/user-attachments/assets/d1b38d7d-5889-4959-8a0e-23cf9b388a75";
 />

  
  Case 2: Improving the function to force setting initial values as true -> The 
llvm IR generated is correct (**hence the wasm module is correct too**)
  ```
  void CompilerInstance::parseLLVMArgs() {
  if (!getFrontendOpts().LLVMArgs.empty()) {
    unsigned NumArgs = getFrontendOpts().LLVMArgs.size();
    auto Args = std::make_unique<const char*[]>(NumArgs + 2);
    Args[0] = "clang (LLVM option parsing)";
    
    auto &RegisteredOptions = llvm::cl::getRegisteredOptions();
    
    for (unsigned i = 0; i != NumArgs; ++i) {
      Args[i + 1] = getFrontendOpts().LLVMArgs[i].c_str();
      
      // Pre-set boolean options directly
      StringRef ArgStr(Args[i + 1]);
      if (ArgStr.starts_with("-")) {
        StringRef OptionName = ArgStr.drop_front(1);
        auto It = RegisteredOptions.find(OptionName);
        if (It != RegisteredOptions.end()) {
          // Cast to bool option and set initial value
          auto *BoolOption = static_cast<llvm::cl::opt<bool> *>(It->second);
          if (BoolOption) {
            BoolOption->setInitialValue(true);
          }
        }
      }
    }
    Args[NumArgs + 1] = nullptr;
    
    llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
  }
}
```
  LLVM IR
  ```
  ; ModuleID = 'incr_module_1'
source_filename = "incr_module_1"
target datalayout = 
"e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-emscripten"
@_ZTIi = external constant ptr
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr 
} { i32 65535, ptr @_GLOBAL__sub_I_incr_module_1, ptr null }]
@__THREW__ = external global i32
define internal void @__stmts__0() #0 personality ptr @__gxx_personality_v0 {
  %1 = alloca ptr, align 4
  %2 = alloca i32, align 4
  %3 = call ptr @__cxa_allocate_exception(i32 4) #5
  store i32 1, ptr %3, align 16
  store i32 0, ptr @__THREW__, align 4
  call cc99 void @__invoke_void_ptr_ptr_ptr(ptr @__cxa_throw, ptr %3, ptr 
@_ZTIi, ptr null)
  %4 = load i32, ptr @__THREW__, align 4
  store i32 0, ptr @__THREW__, align 4
  %5 = icmp eq i32 %4, 1
  br i1 %5, label %6, label %17
6:                                                ; preds = %0
  %7 = call ptr @__cxa_find_matching_catch_3(ptr null)
  %8 = insertvalue { ptr, i32 } poison, ptr %7, 0
  %9 = call i32 @getTempRet0()
  %10 = insertvalue { ptr, i32 } %8, i32 %9, 1
  %11 = extractvalue { ptr, i32 } %10, 0
  store ptr %11, ptr %1, align 4
  %12 = extractvalue { ptr, i32 } %10, 1
  store i32 %12, ptr %2, align 4
  br label %13
13:                                               ; preds = %6
  %14 = load ptr, ptr %1, align 4
  %15 = call ptr @__cxa_begin_catch(ptr %14) #5
  call void @__cxa_end_catch()
  br label %16
16:                                               ; preds = %13
  ret void
17:                                               ; preds = %0
  unreachable
}
declare ptr @__cxa_allocate_exception(i32) #0
declare void @__cxa_throw(ptr, ptr, ptr) #0
declare i32 @__gxx_personality_v0(...) #0
declare ptr @__cxa_begin_catch(ptr) #0
declare void @__cxa_end_catch() #0
; Function Attrs: noinline
define internal void @_GLOBAL__sub_I_incr_module_1() #1 {
  call void @__stmts__0()
  ret void
}
; Function Attrs: nounwind
declare i32 @getTempRet0() #2
declare void @__invoke_void_ptr_ptr_ptr(ptr, ptr, ptr, ptr) #3
declare ptr @__cxa_find_matching_catch_3(ptr) #4
attributes #0 = { 
"target-features"="-atomics,+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,-exception-handling,-extended-const,-fp16,-gc,-multimemory,+multivalue,+mutable-globals,+nontrapping-fptoint,+reference-types,-relaxed-simd,+sign-ext,-simd128,-tail-call,-wide-arithmetic"
 }
attributes #1 = { noinline "no-trapping-math"="true" 
"stack-protector-buffer-size"="8" 
"target-features"="-atomics,+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,-exception-handling,-extended-const,-fp16,-gc,-multimemory,+multivalue,+mutable-globals,+nontrapping-fptoint,+reference-types,-relaxed-simd,+sign-ext,-simd128,-tail-call,-wide-arithmetic"
 }
attributes #2 = { nounwind 
"target-features"="-atomics,+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,-exception-handling,-extended-const,-fp16,-gc,-multimemory,+multivalue,+mutable-globals,+nontrapping-fptoint,+reference-types,-relaxed-simd,+sign-ext,-simd128,-tail-call,-wide-arithmetic"
 }
attributes #3 = { 
"target-features"="-atomics,+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,-exception-handling,-extended-const,-fp16,-gc,-multimemory,+multivalue,+mutable-globals,+nontrapping-fptoint,+reference-types,-relaxed-simd,+sign-ext,-simd128,-tail-call,-wide-arithmetic"
 "wasm-import-module"="env" "wasm-import-name"="__invoke_void_ptr_ptr_ptr" }
attributes #4 = { 
"target-features"="-atomics,+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,-exception-handling,-extended-const,-fp16,-gc,-multimemory,+multivalue,+mutable-globals,+nontrapping-fptoint,+reference-types,-relaxed-simd,+sign-ext,-simd128,-tail-call,-wide-arithmetic"
 "wasm-import-module"="env" "wasm-import-name"="__cxa_find_matching_catch_3" }
attributes #5 = { nounwind }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}
!llvm.ident = !{!10}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 1, !"wasm-feature-bulk-memory", i32 43}
!2 = !{i32 1, !"wasm-feature-bulk-memory-opt", i32 43}
!3 = !{i32 1, !"wasm-feature-call-indirect-overlong", i32 43}
!4 = !{i32 1, !"wasm-feature-multivalue", i32 43}
!5 = !{i32 1, !"wasm-feature-mutable-globals", i32 43}
!6 = !{i32 1, !"wasm-feature-nontrapping-fptoint", i32 43}
!7 = !{i32 1, !"wasm-feature-reference-types", i32 43}
!8 = !{i32 1, !"wasm-feature-sign-ext", i32 43}
!9 = !{i32 1, !"wasm-feature-shared-mem", i32 45}
!10 = !{!"clang version 23.0.0git ([email protected]:anutosh491/llvm-project.git 
d023577ef98490a097108eacaecd2dd114fd7974)"}
```

An output is not expected here but this works well.
<img width="2146" height="632" alt="image" 
src="https://github.com/user-attachments/assets/384b3447-f6d0-4acc-8513-b4404b867430";
 />

So my question is : Is there something resetting the values (mllvm args) in 
case of clang-repl maybe after every input or something ? I am confused & hence 
had to hack around with the `parseLLVMArgs` functions.
  
  

https://github.com/llvm/llvm-project/pull/132670
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to