Hi,
I want to improve debug information, user error messages and fix a bug for OpenMP offloading for flang with attached -save-temps
flag, I describe the motivation, root cause in the next paragraphs and I provide three possible solutions. Please choose the best option.
Motivation:
I would like to point out that Flang behaves differently when -save-temps flag is specified by the user. I have observed 3 issues which are visible when -save-flag
is set.
-
Different information about error location when
-save-temps
flag is set
Information about error location is dependent on-save-temps
flag. If the user sets-save-temps
then the error is reported in the preprocessed file. If-save-temps
flag is not set then error is reported in source file. Clang always reports an error in the source file.Example:
Source code:
subroutine test
implicit none
integer :: i
ij=10
print *,i
end subroutine
Output of flang-new -save-temps -c test.f95 :
error: Semantic errors in test.i
./test.i:5:7: error: No explicit type declared for 'ij'
Output of flang-new -c test.f95:
error: Semantic errors in test.f95
./test.f95:4:3: error: No explicit type declared for 'ij'
As you can see the source location of the error is different (different file, line and column)
-
Different debug info for -save-temps flag:
If you attach-save-temps
flag then the debug info wil point to the location inside preprocessed file not the original source code. If we do not attach-save-temps
then debug info will point out to original source code.Clang always points debug info to the original source code.
-
OpenMP offloading function mismatch for -save-temps flag
The-save-temps
flag breaks code generation for OpenMP offloading. OpenMP requires separate preprocessing for host and the device code.
That’s why we generate two preprocessed files for OpenMP offloading:test_host.i
andtest_device.i
. We use the name of the source file as the parameter for generation of an unique name of the kernel function which corresponds to givenpragma omp target
. Clang always takes as the parameter the name of the source file. Flang uses two different files for -save-temps flag (test_host.i and test_device.i) as the input when it generates the host call and target function. In consequence we have a function names mismatch:
on the host side:
call omp_kernel_1111 ()
whereas the device kernel has different name:
omp_kernel_2222 () { ... }
We have no function name mismatch for Flang when we do not set -save-temps flag because the function names is generated on the basis of the source file not the preprocessed files.
Root cause:
The flang driver provides different set of flang-new -fc1
commands. When -save-temps
flag is set we have separate preprocessor step:
For flang-new -save-temps -v -c test.f95
we see the following flang-new -fc1
commands:
flang-new -fc1 -E /* other flags */ -o test.i
flang-new -fc1 /*other flags */ -o test.bc -x f95 test.i
If flag -save-temps
is not set then we have no separated preprocessor step:
flang-new -fc1 /*other flags */ -o test.o -x f95-cpp-input test.f95
Discussion about possible solutions:
A) Merge preprocessor step with LLVM IR code generation.
If user specifies -save temps
then flang will launch one command:
flang-new -fc1 /*other flags */ -o test.bc -x f95 test.f95
instead of:
flang-new -fc1 -E /* other flags */ -o test.i
flang-new -fc1 /*other flags */ -o test.bc -x f95 test.i
Advantages:
- No modification of Flang Preprocessor.
Disadvantages:
- Possible code duplication with Clang. IIRC Flang uses parts of Clang driver to control compilation phases.
B) Modify flang preprocessor so that it will use source location from original source file not the preprocessed file.
Advantages:
a) No modification of Flang/Clang driver
Disadvantages:
a) Flang preprocessor attaches limited information about origins of the preprocessed code.
b) User can disable information about the source file in preprocessor output. If user specifies flang-new -P -save-temps then we have no information about original code loacation and all problems mentioned in previous section are back.
C) Provide information about original source file by additional -fc1 flag -main-file-name
Advantages:
a) Allow to bypass issues mentioned in solutions 1 and 2.
b) This solution allows me to sollve an OpenMP offloading function name mismatch without large modification of Flang source code.
Disadvantages:
a) It’s a bypass solution and it complicates compiler logic. We still could have inaccurate information about line/column of the code symbol if the parser modifies the layout of the source code (Actually flang-new -E generates reformatted code so the line number/column can be inaccurate with -save-temps flag).
Thank you for reading so far. Please share your feedback.