oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.39k stars 1.64k forks source link

Native-image fails at LLVM optimization when compiling @CEntryPoint function that returns CCharPointer #3202

Open jperedadnr opened 3 years ago

jperedadnr commented 3 years ago

Describe the issue

Running native-image with -Dsvm.platform=org.graalvm.nativeimage.Platform$IOS_AARCH64 and a simple class that contains:

public static void main(String[] args) {
        System.out.println("Hello World!! ");
        System.exit(0);
}

@CEntryPoint(name = "myFunc")
public static CCharPointer myFunc(IsolateThread thread) {
    final CTypeConversion.CCharPointerHolder holder = CTypeConversion.toCString("Hello from Java");
    return holder.get();
}

fails with:

Fatal error:org.graalvm.compiler.debug.GraalError: LLVM optimization failed for com.oracle.svm.reflect.HelloWorld_myFunc_3bd8684324c208b69be73dc47f34e3eeb7d1966c.invoke(java.lang.Object, java.lang.Object[]) (/HelloWorld/target/client/arm64-ios/gvm/tmp/SVM-1612910093370/llvm/f2101.bc): 1

Steps to reproduce the issue Please include both build steps as well as run steps

  1. Create HelloWorld.java class with above content, adding org.graalvm.nativeimage:library-support:21.0.0 dependency.
  2. mvn clean compile
  3. Run native-image
$GRAALVM_HOME/bin/native-image  -H:+SharedLibrary -H:-DeadlockWatchdogExitOnTimeout -H:DeadlockWatchdogInterval=0 -H:+RemoveSaturatedTypeFlows -H:+ExitAfterRelocatableImageWrite --features=org.graalvm.home.HomeFinderFeature -H:CompilerBackend=llvm -H:-SpawnIsolates -H:PageSize=16384 -Dsvm.targetName=iOS -Dsvm.targetArch=arm64  -Dsvm.platform=org.graalvm.nativeimage.Platform$IOS_AARCH64 -cp target/classes -H:LLVMMaxFunctionsPerBatch=1 hello.HelloWorld

Describe GraalVM and your environment:

More details

Native-image log:

[hello.helloworld:16594]    classlist:   1,454.27 ms,  0.96 GB
[hello.helloworld:16594]        (cap):     207.83 ms,  0.96 GB
[hello.helloworld:16594]        setup:   1,916.29 ms,  0.96 GB
[hello.helloworld:16594]     (clinit):     480.10 ms,  4.90 GB
[hello.helloworld:16594]   (typeflow):  16,049.82 ms,  4.90 GB
[hello.helloworld:16594]    (objects):  28,975.79 ms,  4.90 GB
[hello.helloworld:16594]   (features):   1,495.04 ms,  4.90 GB
[hello.helloworld:16594]     analysis:  47,697.96 ms,  4.90 GB
[hello.helloworld:16594]     universe:   1,003.98 ms,  4.89 GB
[hello.helloworld:16594]      (parse):   3,491.48 ms,  4.89 GB
[hello.helloworld:16594]     (inline):   7,819.13 ms,  5.56 GB
[hello.helloworld:16594]    (compile):  29,731.11 ms,  5.20 GB
[hello.helloworld:16594]    (bitcode):   2,011.60 ms,  5.20 GB
[hello.helloworld:16594]    (prelink):       0.04 ms,  5.20 GB
[hello.helloworld:16594]       (llvm):  17,522.79 ms,  5.20 GB
[hello.helloworld:16594]      compile:  60,968.48 ms,  5.20 GB
Fatal error:org.graalvm.compiler.debug.GraalError: LLVM optimization failed for com.oracle.svm.reflect.HelloWorld_myFunc_3bd8684324c208b69be73dc47f34e3eeb7d1966c.invoke(java.lang.Object, java.lang.Object[]) (/HelloWorld/target/client/arm64-ios/gvm/tmp/SVM-1612910093370/llvm/f2101.bc): 1
Command: opt -mem2reg -rewrite-statepoints-for-gc -always-inline -o f2101o.bc f2101.bc
    at com.oracle.svm.core.graal.llvm.LLVMNativeImageCodeCache.llvmOptimize(LLVMNativeImageCodeCache.java:242)
    at com.oracle.svm.core.graal.llvm.LLVMNativeImageCodeCache.lambda$compileBitcodeBatches$5(LLVMNativeImageCodeCache.java:180)
    at com.oracle.graal.pointsto.util.CompletionExecutor.lambda$execute$0(CompletionExecutor.java:173)
    at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Error: Image build request failed with exit status 1

Running opt with the failing bc file (see attached):

$GRAALVM_HOME/lib/llvm/bin/opt -mem2reg -rewrite-statepoints-for-gc -always-inline -o f2101o.bc f2101.bc
Function return type does not match operand type of return inst!
  ret { i64, i64 } %51
 { i64, i8 addrspace(1)* }Function return type does not match operand type of return inst!
  ret { i64, i64 } %53
 { i64, i8 addrspace(1)* }$GRAALVM_HOME/lib/llvm/bin/opt: f2101.bc: error: input module is broken!

the relevant parts of f2101.ll are:

 92   br i1 %47, label %B13, label %B14, !prof !4 
 93 
 94 B13:                                              ; preds = %B12
 95   %48 = call { i64 } @Safepoint_enterSlowPathSafepointCheck_6065687f19bea4c522a5ace56dcc1231d113e373(i64 %90) #6
 96   %49 = extractvalue { i64 } %48, 0
 97   %50 = insertvalue { i64, i64 } zeroinitializer, i64 %49, 0
 98   %51 = insertvalue { i64, i64 } %50, i64 %91, 1
 99   ret { i64, i64 } %51
100 
101 B14: 

File: f2101.bc.zip

munishchouhan commented 3 years ago

@jperedadnr thanks for reporting the issue I tried, but I got another error:

ative-image  -H:+SharedLibrary -H:-DeadlockWatchdogExitOnTimeout -H:DeadlockWatchdogInterval=0 -H:+RemoveSaturatedTypeFlows -H:+ExitAfterRelocatableImageWrite --features=org.graalvm.home.HomeFinderFeature -H:CompilerBackend=llvm -H:-SpawnIsolates -H:PageSize=16384 -Dsvm.targetName=iOS -Dsvm.targetArch=arm64  -Dsvm.platform=org.graalvm.nativeimage.Platform$IOS_AARCH64 -cp target/classes -H:LLVMMaxFunctionsPerBatch=1 hello.HelloWorld
Exception in thread "main" com.oracle.svm.core.util.UserError$UserException: Could not instantiate platform class org.graalvm.nativeimage.Platform. Ensure the class is not abstract and has a no-argument constructor.
        at com.oracle.svm.core.util.UserError.abort(UserError.java:82)
        at com.oracle.svm.hosted.NativeImageGenerator.loadPlatform(NativeImageGenerator.java:329)
        at com.oracle.svm.hosted.NativeImageGenerator.getTargetPlatform(NativeImageGenerator.java:359)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner.installNativeImageClassLoader(NativeImageGeneratorRunner.java:176)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:118)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner$JDK9Plus.main(NativeImageGeneratorRunner.java:561)
Caused by: java.lang.NoSuchMethodException: org.graalvm.nativeimage.Platform.<init>()
        at java.base/java.lang.Class.getConstructor0(Class.java:3349)
        at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
        at com.oracle.svm.util.ReflectionUtil.lookupConstructor(ReflectionUtil.java:69)
        at com.oracle.svm.util.ReflectionUtil.newInstance(ReflectionUtil.java:80)
        at com.oracle.svm.hosted.NativeImageGenerator.loadPlatform(NativeImageGenerator.java:327)
        ... 4 more
Error: Image build request failed with exit status 1
johanvos commented 3 years ago

You probably need to escape the $ in svm.platform=org.graalvm.nativeimage.Platform$IOS_AARCH64

loicottet commented 3 years ago

@jperedadnr The problem happens when calling a method returning a WordBase type through reflection. I'm trying to see how I can get this to work. However I'm surprised to see this method being registered for reflection, are you using a reflection configuration file somewhere?

jperedadnr commented 3 years ago

Yes, we use reflection with the usual Java classes.

Find attached the config files:

Config files.zip