Skip to content

Conversation

@shivaramaarao
Copy link
Contributor

When a macro is followed by OpenMP pragma it is considered as comment and ignored. The function IsCompilerDirectiveSentinel expects the compiler directive argument without the prefix comment character. This is fixed in this commit.

Fixes #117693

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Jan 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 15, 2025

@llvm/pr-subscribers-flang-parser

Author: None (shivaramaarao)

Changes

When a macro is followed by OpenMP pragma it is considered as comment and ignored. The function IsCompilerDirectiveSentinel expects the compiler directive argument without the prefix comment character. This is fixed in this commit.

Fixes #117693


Full diff: https://github.com/llvm/llvm-project/pull/123035.diff

2 Files Affected:

  • (modified) flang/lib/Parser/prescan.cpp (+1-1)
  • (added) flang/test/Preprocessing/bug117693.f90 (+14)
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index c5939a1e0b6c2c..50a9193d0a57b3 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -511,7 +511,7 @@ bool Prescanner::MustSkipToEndOfLine() const {
   if (inFixedForm_ && column_ > fixedFormColumnLimit_ && !tabInCurrentLine_) {
     return true; // skip over ignored columns in right margin (73:80)
   } else if (*at_ == '!' && !inCharLiteral_) {
-    return !IsCompilerDirectiveSentinel(at_);
+    return !IsCompilerDirectiveSentinel(at_+1);
   } else {
     return false;
   }
diff --git a/flang/test/Preprocessing/bug117693.f90 b/flang/test/Preprocessing/bug117693.f90
new file mode 100644
index 00000000000000..ced7927606e623
--- /dev/null
+++ b/flang/test/Preprocessing/bug117693.f90
@@ -0,0 +1,14 @@
+! RUN: %flang -fopenmp -E %s 2>&1 | FileCheck %s
+!CHECK: !$OMP DO SCHEDULE(STATIC)
+program main
+IMPLICIT NONE
+INTEGER:: I
+#define OMPSUPPORT
+!$ INTEGER :: omp_id
+!$OMP PARALLEL DO
+OMPSUPPORT !$OMP DO SCHEDULE(STATIC)
+DO I=1,100
+print *, omp_id
+ENDDO
+!$OMP END PARALLEL DO
+end program

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff eb96c8c105226956c8ed5ab30699206f53de74f7 bfc3d201256ecbdc77402758dae955048384ef78 --extensions cpp -- flang/lib/Parser/prescan.cpp
View the diff from clang-format here.
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 50a9193d0a..b928ed03a2 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -511,7 +511,7 @@ bool Prescanner::MustSkipToEndOfLine() const {
   if (inFixedForm_ && column_ > fixedFormColumnLimit_ && !tabInCurrentLine_) {
     return true; // skip over ignored columns in right margin (73:80)
   } else if (*at_ == '!' && !inCharLiteral_) {
-    return !IsCompilerDirectiveSentinel(at_+1);
+    return !IsCompilerDirectiveSentinel(at_ + 1);
   } else {
     return false;
   }

@shivaramaarao
Copy link
Contributor Author

I am seeing a side effect with this change.

program main
INTEGER::n
!$OMP PARALLEL DO SCHEDULE(STATIC) DEFAULT(NONE) !$OMP PRIVATE
DO I=1,100
ENDDO
END PROGRAM

The comment is not getting ignored by the preprocessor.

@shivaramaarao
Copy link
Contributor Author

I do see more cases to be handled. currently for the source lines that begins with MACRO, characters after "!" are discarded and many issues are hidden.
We need to handle
a) comments after compiler directives
e.g MACRO !$ OMP
b) conditional statements
e.g MACRO !$ ndim=0
c) continuation lines
e.g MACRO !$OMP !comment &
MACRO !$OMP
d) compiler directives that are actually comments
MACRO !$OMP !$OMP

When I was looking into the code, I feel we might need to make design changes to existing implementation that handles macro replacement to consider all these cases. Can someone please comment on this?

@shivaramaarao
Copy link
Contributor Author

The recent pull request addresses the issue with comments and continuation lines. The conditional compilation issue will be addressed as separate pull request.

@shivaramaarao shivaramaarao reopened this Feb 5, 2025
@shivaramaarao
Copy link
Contributor Author

Is the PR ok to submit? If So, I can ask some of our AMD engineers to commit and we will follow up on any regressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:parser flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flang] preprocessor removes OPENMP pragmas

3 participants