Giter VIP home page Giter VIP logo

Comments (5)

WojciechMazur avatar WojciechMazur commented on May 28, 2024

Seems like the code gen should be adapted to the non MSVC compilers, unfortunately it was never tested when compiling for windows using gnu compilers. Current code gen is based on the outputs of clang++ -S -emit-llvm as it's using the C++ exception handling underneath. In long term we should ditch it to use our own exception handling using involving a dedicated personality. Until that point we should check what outputs does c++ produce and add a dedicated version of PlatformCompat for Windows GNU.

from scala-native.

ekrich avatar ekrich commented on May 28, 2024

Current code gen is based on the outputs of clang++ -S -emit-llvm as it's using the C++ exception handling underneath. In long term we should ditch it to use our own exception handling using involving a dedicated personality.

Amen to that.

from scala-native.

avdv avatar avdv commented on May 28, 2024

Here's the relevant code from gcc: https://github.com/gcc-mirror/gcc/blob/8677182f32234786fccce25583232ec5181dde75/libstdc%2B%2B-v3/libsupc%2B%2B/eh_personality.cc#L336-L345

FTR, here are the rules I use with Byteman to make my project link:

RULE use UnixCompat
CLASS scala.scalanative.codegen.llvm.AbstractCodeGen
METHOD os()
AT ENTRY
IF true
DO return new scala.scalanative.codegen.llvm.compat.os.UnixCompat($this)
ENDRULE

RULE gxx personality
CLASS scala.scalanative.codegen.llvm.compat.os.UnixCompat
METHOD osPersonalityType()
AT ENTRY
IF true
DO return "@__gxx_personality_seh0"
ENDRULE

According to my testing it's working just fine.

from scala-native.

avdv avatar avdv commented on May 28, 2024

For this source file:

#include <cstdio>

double div(double numerator, double denominator) {
  try {
    // throw an exception if denominator is 0
    if (denominator == 0)
      throw 0;

    // not executed if denominator is 0
    return numerator / denominator;
  }

  catch (int num_exception) {
    std::puts("caught exception");
    return 42.0;
  }
}

I ran clang++ -S -emit-llvm on Linux (clang version 16.0.6) targeting x86_64-unknown-linux-gnu and x86_64-unknown-windows-gnu respectively.

Here's the diff:

--- div_linux.ll	2024-04-05 18:29:01.901615817 +0200
+++ div_windows.ll	2024-04-05 18:28:48.616259588 +0200
@@ -1,13 +1,13 @@
 ; ModuleID = 'div.cpp'
 source_filename = "div.cpp"
-target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
-target triple = "x86_64-unknown-linux-gnu"
+target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-windows-gnu"
 
 @_ZTIi = external constant ptr
 @.str = private unnamed_addr constant [17 x i8] c"caught exception\00", align 1
 
 ; Function Attrs: mustprogress sspstrong uwtable
-define noundef double @_Z3divdd(double noundef %0, double noundef %1) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
+define dso_local noundef double @_Z3divdd(double noundef %0, double noundef %1) local_unnamed_addr #0 personality ptr @__gxx_personality_seh0 {
   %3 = fcmp oeq double %1, 0.000000e+00
   br i1 %3, label %4, label %15
 
@@ -47,21 +47,21 @@
   unreachable
 }
 
-declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr
+declare dso_local ptr @__cxa_allocate_exception(i64) local_unnamed_addr
 
-declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr
+declare dso_local void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr
 
-declare i32 @__gxx_personality_v0(...)
+declare dso_local i32 @__gxx_personality_seh0(...)
 
 ; Function Attrs: nofree nosync nounwind memory(none)
 declare i32 @llvm.eh.typeid.for(ptr) #1
 
-declare ptr @__cxa_begin_catch(ptr) local_unnamed_addr
+declare dso_local ptr @__cxa_begin_catch(ptr) local_unnamed_addr
 
 ; Function Attrs: nofree nounwind
-declare noundef i32 @puts(ptr nocapture noundef readonly) local_unnamed_addr #2
+declare dso_local noundef i32 @puts(ptr nocapture noundef readonly) local_unnamed_addr #2
 
-declare void @__cxa_end_catch() local_unnamed_addr
+declare dso_local void @__cxa_end_catch() local_unnamed_addr
 
 attributes #0 = { mustprogress sspstrong uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="4" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
 attributes #1 = { nofree nosync nounwind memory(none) }
@@ -72,7 +72,7 @@
 !llvm.module.flags = !{!0, !1, !2}
 !llvm.ident = !{!3}
 
-!0 = !{i32 1, !"wchar_size", i32 4}
+!0 = !{i32 1, !"wchar_size", i32 2}
 !1 = !{i32 8, !"PIC Level", i32 2}
 !2 = !{i32 7, !"uwtable", i32 2}
 !3 = !{!"clang version 16.0.6"}

So, AFAICS, the only change seems to be a different personality and that it is using dso_local. Is this enough information to create a fix? Would I just have to add another WindowsGnuCompat instance for this? TIA

from scala-native.

WojciechMazur avatar WojciechMazur commented on May 28, 2024

Yes, seems like it's all that is required. I'm right now stuffed with other work, and won't be able to create fix in the next 2 weeks. I'd really appreciate PRs. Thank you for investigating this topic!

from scala-native.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.