-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[MLIR][OpenMP] Add canonical loop LLVM-IR lowering #147069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Meinersbur
merged 6 commits into
main
from
users/meinersbur/flang_canonical-loop_ops-lowering
Jul 11, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4a18faa
Emit named operand indices
Meinersbur c7ed06d
Add omp.canonical_loop and omp.unroll modelling
Meinersbur da2613d
omp.canonical_loop and omp.unroll_heuristic lowering
Meinersbur 7a4dff7
Merge branch 'main' into HEAD
Meinersbur 3dda96f
Merge branch 'users/meinersbur/flang_canonical-loop_ops' into HEAD
Meinersbur 7622bf4
Merge branch 'main' into HEAD
Meinersbur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// Test lowering of standalone omp.canonical_loop | ||
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s | ||
|
||
// CHECK-LABEL: define void @anon_loop( | ||
// CHECK-SAME: ptr %[[ptr:.+]], | ||
// CHECK-SAME: i32 %[[tc:.+]]) { | ||
// CHECK-NEXT: br label %omp_omp.loop.preheader | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.preheader: | ||
// CHECK-NEXT: br label %omp_omp.loop.header | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.header: | ||
// CHECK-NEXT: %omp_omp.loop.iv = phi i32 [ 0, %omp_omp.loop.preheader ], [ %omp_omp.loop.next, %omp_omp.loop.inc ] | ||
// CHECK-NEXT: br label %omp_omp.loop.cond | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.cond: | ||
// CHECK-NEXT: %omp_omp.loop.cmp = icmp ult i32 %omp_omp.loop.iv, %[[tc]] | ||
// CHECK-NEXT: br i1 %omp_omp.loop.cmp, label %omp_omp.loop.body, label %omp_omp.loop.exit | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.body: | ||
// CHECK-NEXT: br label %omp.loop.region | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp.loop.region: | ||
// CHECK-NEXT: store float 4.200000e+01, ptr %[[ptr]], align 4 | ||
// CHECK-NEXT: br label %omp.region.cont | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp.region.cont: | ||
// CHECK-NEXT: br label %omp_omp.loop.inc | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.inc: | ||
// CHECK-NEXT: %omp_omp.loop.next = add nuw i32 %omp_omp.loop.iv, 1 | ||
// CHECK-NEXT: br label %omp_omp.loop.header | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.exit: | ||
// CHECK-NEXT: br label %omp_omp.loop.after | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.after: | ||
// CHECK-NEXT: ret void | ||
// CHECK-NEXT: } | ||
llvm.func @anon_loop(%ptr: !llvm.ptr, %tc : i32) -> () { | ||
omp.canonical_loop %iv : i32 in range(%tc) { | ||
%val = llvm.mlir.constant(42.0 : f32) : f32 | ||
llvm.store %val, %ptr : f32, !llvm.ptr | ||
omp.terminator | ||
} | ||
llvm.return | ||
} | ||
|
||
|
||
|
||
// CHECK-LABEL: define void @trivial_loop( | ||
// CHECK-SAME: ptr %[[ptr:.+]], | ||
// CHECK-SAME: i32 %[[tc:.+]]) { | ||
// CHECK-NEXT: br label %omp_omp.loop.preheader | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.preheader: | ||
// CHECK-NEXT: br label %omp_omp.loop.header | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.header: | ||
// CHECK-NEXT: %omp_omp.loop.iv = phi i32 [ 0, %omp_omp.loop.preheader ], [ %omp_omp.loop.next, %omp_omp.loop.inc ] | ||
// CHECK-NEXT: br label %omp_omp.loop.cond | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.cond: | ||
// CHECK-NEXT: %omp_omp.loop.cmp = icmp ult i32 %omp_omp.loop.iv, %[[tc]] | ||
// CHECK-NEXT: br i1 %omp_omp.loop.cmp, label %omp_omp.loop.body, label %omp_omp.loop.exit | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.body: | ||
// CHECK-NEXT: br label %omp.loop.region | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp.loop.region: | ||
// CHECK-NEXT: store float 4.200000e+01, ptr %[[ptr]], align 4 | ||
// CHECK-NEXT: br label %omp.region.cont | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp.region.cont: | ||
// CHECK-NEXT: br label %omp_omp.loop.inc | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.inc: | ||
// CHECK-NEXT: %omp_omp.loop.next = add nuw i32 %omp_omp.loop.iv, 1 | ||
// CHECK-NEXT: br label %omp_omp.loop.header | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.exit: | ||
// CHECK-NEXT: br label %omp_omp.loop.after | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.after: | ||
// CHECK-NEXT: ret void | ||
// CHECK-NEXT: } | ||
llvm.func @trivial_loop(%ptr: !llvm.ptr, %tc : i32) -> () { | ||
%cli = omp.new_cli | ||
omp.canonical_loop(%cli) %iv : i32 in range(%tc) { | ||
%val = llvm.mlir.constant(42.0 : f32) : f32 | ||
llvm.store %val, %ptr : f32, !llvm.ptr | ||
omp.terminator | ||
} | ||
llvm.return | ||
} | ||
|
||
|
||
// CHECK-LABEL: define void @nested_loop( | ||
// CHECK-SAME: ptr %[[ptr:.+]], i32 %[[outer_tc:.+]], i32 %[[inner_tc:.+]]) { | ||
// CHECK-NEXT: br label %omp_omp.loop.preheader | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.preheader: | ||
// CHECK-NEXT: br label %omp_omp.loop.header | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.header: | ||
// CHECK-NEXT: %omp_omp.loop.iv = phi i32 [ 0, %omp_omp.loop.preheader ], [ %omp_omp.loop.next, %omp_omp.loop.inc ] | ||
// CHECK-NEXT: br label %omp_omp.loop.cond | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.cond: | ||
// CHECK-NEXT: %omp_omp.loop.cmp = icmp ult i32 %omp_omp.loop.iv, %[[outer_tc]] | ||
// CHECK-NEXT: br i1 %omp_omp.loop.cmp, label %omp_omp.loop.body, label %omp_omp.loop.exit | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.body: | ||
// CHECK-NEXT: br label %omp.loop.region | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp.loop.region: | ||
// CHECK-NEXT: br label %omp_omp.loop.preheader1 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.preheader1: | ||
// CHECK-NEXT: br label %omp_omp.loop.header2 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.header2: | ||
// CHECK-NEXT: %omp_omp.loop.iv8 = phi i32 [ 0, %omp_omp.loop.preheader1 ], [ %omp_omp.loop.next10, %omp_omp.loop.inc5 ] | ||
// CHECK-NEXT: br label %omp_omp.loop.cond3 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.cond3: | ||
// CHECK-NEXT: %omp_omp.loop.cmp9 = icmp ult i32 %omp_omp.loop.iv8, %[[inner_tc]] | ||
// CHECK-NEXT: br i1 %omp_omp.loop.cmp9, label %omp_omp.loop.body4, label %omp_omp.loop.exit6 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.body4: | ||
// CHECK-NEXT: br label %omp.loop.region12 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp.loop.region12: | ||
// CHECK-NEXT: store float 4.200000e+01, ptr %[[ptr]], align 4 | ||
// CHECK-NEXT: br label %omp.region.cont11 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp.region.cont11: | ||
// CHECK-NEXT: br label %omp_omp.loop.inc5 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.inc5: | ||
// CHECK-NEXT: %omp_omp.loop.next10 = add nuw i32 %omp_omp.loop.iv8, 1 | ||
// CHECK-NEXT: br label %omp_omp.loop.header2 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.exit6: | ||
// CHECK-NEXT: br label %omp_omp.loop.after7 | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.after7: | ||
// CHECK-NEXT: br label %omp.region.cont | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp.region.cont: | ||
// CHECK-NEXT: br label %omp_omp.loop.inc | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.inc: | ||
// CHECK-NEXT: %omp_omp.loop.next = add nuw i32 %omp_omp.loop.iv, 1 | ||
// CHECK-NEXT: br label %omp_omp.loop.header | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.exit: | ||
// CHECK-NEXT: br label %omp_omp.loop.after | ||
// CHECK-EMPTY: | ||
// CHECK-NEXT: omp_omp.loop.after: | ||
// CHECK-NEXT: ret void | ||
// CHECK-NEXT: } | ||
llvm.func @nested_loop(%ptr: !llvm.ptr, %outer_tc : i32, %inner_tc : i32) -> () { | ||
%outer_cli = omp.new_cli | ||
%inner_cli = omp.new_cli | ||
omp.canonical_loop(%outer_cli) %outer_iv : i32 in range(%outer_tc) { | ||
omp.canonical_loop(%inner_cli) %inner_iv : i32 in range(%inner_tc) { | ||
%val = llvm.mlir.constant(42.0 : f32) : f32 | ||
llvm.store %val, %ptr : f32, !llvm.ptr | ||
omp.terminator | ||
} | ||
omp.terminator | ||
} | ||
llvm.return | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @Meinersbur for the late comment, was just now able to take a brief look at this. I don't love the idea of introducing OpenMP dialect-specific handling into this generic MLIR translation class, did you consider using stack frames (
OpenMPLoopInfoStackFrame
) similarly to what is done toomp.loop_nest
instead?The idea there was to create a stack frame holding a single null
llvm::CanonicalLoopInfo *
when finding the top-level loop wrapper (which we know is associated to a singleomp.loop_nest
), then populating it when that operation is translated to LLVM IR and finally updating it with the processing of each loop wrapper. It would probably work somewhat differently in this case, but if we could use a similar method or even reuse/extend what we have, that might be helpful when supporting e.g. worksharing of transformed loops. Plus, I think we should really try to avoid dialect-specific handling here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Post-commit reviews are very welcome!
Since we need a namespace for
omp.new_cli
mappings, a potentialStateStackFrameBase
would need to be the top of the ModuleTranslation/function's stack frame. That requires additional code during function creation, i.e. there is still OpenMP-specific handling in otherwise LLVM dialect-centric translation. As being mandatory, it would not be much different than adding the namespace-lookup object (be it a class derived from StateStackFrameBase or the DenseMap directly) as a member of the ModuleTranslation class.There is an
LLVMTranslationInterface iface
field in ModuleTranslation which looked promising at first, but also only reacts when encountering a dialect's operation or attribute.Any other ideas? I could hide the namespace lookup object from
ModuleTranslation.h
by using the pImpl idiom.The abstraction seems to have been broken anyway. ModuleTranslation already has a
getOpenMPBuilder()
member and has severallookupXYZ
members. MLIR could have translated to the LLVM Dialect in an iterative loweing step, but this idea was not followed in a compromise in order to use the OpenMPIRBuilder. Hence, ModuleTranslation will always require some OpenMP-specific code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that the LLVMTranslationDialectInterface was introduced for OpenMP (https://reviews.llvm.org/D96504) I could try to extend it with a callback on function creation. It could also hide
getOpenMPBuilder()
completely from ModuleTranslation. But that should probably be discussed with the MLIR community.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One idea that may work for this would be to introduce yet another operation to the dialect to serve as a context where loop transformations are performed. Something like this:
Here, the
omp.loop_context
op would take the place of the currentomp.loop_nest
, so it integrates seamlessly with all loop wrappers, and it would have a single block region containing onlyomp.new_cli
,omp.canonical_loop
and loop transformation operations. That would give us a place to create a stack frame holding all information needed to handle loop transformations, and perhaps even let us remove theomp.new_cli
and instead make CLIs entry block arguments: