Project

General

Profile

Get a cod file from cpparma64?

Added by Rochus Keller over 1 year ago

I have issues with generating correct IR for arma64 with my version of chibicc. Interestingly, also the binary generated from my local cpparma64 (v0.0.40) which I run with egcs on my local Linux i386 machine crashes when run on arma64. Meanwhile I compiled a full ECS version (0.0.41) directly on the arma64 machine and the binaries generated with this version of cpparma64 don't crash. So I would like to know how the IR generated by this version of cpparma64 looks, so I can correct my code generator correspondingly.

Is there a way to make cpparma64 also generate a cod file (i.e. not only an assembly listing and map file) in addition to the object file?


Replies (15)

RE: Get a cod file from cpparma64? - Added by Florian Negele over 1 year ago

Apply the attached patch file. It basically combines the code back-end with any other back-end in the compiler driver (tools/compiler.cpp).

RE: Get a cod file from cpparma64? - Added by Rochus Keller over 1 year ago

Great, thanks, that worked!

I guess it would be quite a bit more compilcated to make this also work for the cross-compiler, isn't it?

In my migrated version I tried this solution which seems to work: https://github.com/rochus-keller/EiGen/blob/5d299773e069d44694a3dcbd4de2620ba5f5a849/compiler.cpp#L227

but it requires a lot of other modifications.

RE: Get a cod file from cpparma64? - Added by Florian Negele over 1 year ago

What cross compiler?

RE: Get a cod file from cpparma64? - Added by Rochus Keller over 1 year ago

What cross compiler?

The ecsd -t arma64linux which I run on my local Linux i386 machine.

But it becomes even stranger. I applied your patch to the ECS version running on my arm64 server and generated a cod, which I compared to the one generated on my machine with chibicc; I didn't find a reason why the latter should not work. This made me couragous so I uploaded all cod files generated with chibicc for arma64 (attached) to my arm64 server and compiled and run them with the ECS running there; and everything worked up to 00009 and 00143.

This result confuses me; when I try to compile e.g. test1.c with my local ecsd -t arma64linux and upload the result to the arm64 server, it crashes; when I compile the cod generated by chibicc with ecsd -t arma64linux, the resulting binary crashes as well on the arm64 server. Do you have an idea what could be the reason? Could it be that I use 0.0.40 locally and 0.0.41 on the arm64 server? I checked the diff but didn't see an obvious cause.

RE: Get a cod file from cpparma64? - Added by Florian Negele over 1 year ago

All test files except cases 00009, 00041, and 00143 run fine under both versions using qemu-aarch64:

for i in *.cod; do ecsd -t arma64linux $i && qemu-aarch64 ${i/.cod/} || echo $i; done

What are the differences of generated intermediate code files? There ideally should not be any.

RE: Get a cod file from cpparma64? - Added by Rochus Keller over 1 year ago

All test files except cases 00009, 00041, and 00143 run fine under both versions using qemu-aarch64:

Ok, thanks for checking.

What are the differences of generated intermediate code files? There ideally should not be any.

Do you mean between the code files chibicc generates for the different CHIBICC_POINTER_WIDTH ... CHIBICC_USE_LINKREGISTER settings?

RE: Get a cod file from cpparma64? - Added by Florian Negele over 1 year ago

No, I mean the intermediate code files generated on your local machine and your server. Are there any differences?

RE: Get a cod file from cpparma64? - Added by Rochus Keller over 1 year ago

ecsd -t arma64linux $i && qemu-aarch64 ${i/.cod/}

Am I right to assume that you're running on Linux x86_64? I run on Linux x86, so that might be a reason for the different outcome. I use the original ECS 0.0.40 version which I comiled and statically linked on a Debian 12 x86 virtual machine using GCC 12.2.

RE: Get a cod file from cpparma64? - Added by Rochus Keller over 1 year ago

No, I mean the intermediate code files generated on your local machine and your server. Are there any differences?

I will check, but for this I first have to rebuild ECS for x86 with your patches. But I will close now for today ;-)

RE: Get a cod file from cpparma64? - Added by Florian Negele over 1 year ago

Am I right to assume that you're running on Linux x86_64? I run on Linux x86, so that might be a reason for the different outcome.

I successfully checked the above command for both versions on Debian 12 using GCC 12.2 once on a x86_64 machine and once on a i686 machine with the same result. There was a compiler issue that caused the compiler to generate a slightly different machine code that behaves exactly the same, see attached patch. After that change even all output files are exactly the same on both systems.

issue.patch (383 Bytes) issue.patch

RE: Get a cod file from cpparma64? - Added by Rochus Keller over 1 year ago

There was a compiler issue that caused the compiler to generate a slightly different machine code

Great, thanks for the patch, I will try it today.

Here for the sake of completeness the complete input and output for the testcases when

1) built on my x86 and run on arm64 real hardware (only 32 of 149 cases successful, see attached testcases_built_on_x86_with_ecs_0.0.40.tar.gz)
2) built and run on arm64 real hardware (146 of 149 successful, see attached testcases_built_on_arm64_with_ecs_0.0.41.tar.gz)

Both tar.gz include the script build_arm64.sh I effectively used for building on the corresponding platform. The *.cod files used as input for testcases_built_on_arm64_with_ecs_0.0.41.tar.gz were the same as in testcases_built_on_x86_with_ecs_0.0.40.tar.gz, but due to your yesterday's patch the arm64 build has overwritten them.

Unfortunately I have no aarch64 qemu on my x86 development machine, so I could not check whether I get a different result when running testcases_built_on_x86_with_ecs_0.0.40.tar.gz on qemu than on the real hardware (Server 2 core Ampere Altra with 4 GB RAM).

RE: Get a cod file from cpparma64? - Added by Rochus Keller over 1 year ago

Success!

I applied the patch to my migrated version of arma64generator.cpp and replaced the cdarma64 app of the original ECS 0.0.40 with my version and then applied the same build steps on x86 and run on arm64 hardware, and now I'm able to reach 146 of 149 success with this build as well. Attached is the build output of my x86 development machine for documentation purpose.

Thanks again for the fix!

I will now focus on the remaining 3 cases which don't work.

RE: Get a cod file from cpparma64? - Added by Rochus Keller over 1 year ago

Meanwhile I analyzed the IR of case 00009 which looks corret to me; I have no idea what I should change to avoid the wrong result on arm64 (252 instead of 0); hints are welcome.

I attached a commented version of the IR file. Didn't look at the generated assembler yet.

RE: Get a cod file from cpparma64? - Added by Florian Negele over 1 year ago

There was an issue regarding modulo instructions that use same register operands. Please apply the attached patch. Thanks for reporting and sorry for the inconveniences.

RE: Get a cod file from cpparma64? - Added by Rochus Keller about 1 year ago

Great, thank you very much.

Now all 149 test cases work on all four tested platforms!

    (1-15/15)