Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

[WIP][MLIR][Flang] WIP additional mapping support #225

Conversation

agozillon
Copy link
Contributor

NOTE: this is a furthering of #217 based on Sergio's rebased of the PR 217 in #224. Starting fresh to maintain the commit history of PR 217.

This patch allows the current milestone 1 example we have to be extended to something like the below:

module test_0
        implicit none
        integer :: decltar_v = 5
    !$omp declare target link(decltar_v)
end module test_0
program main  
        integer :: hostArray(10), errors = 0  
        do i = 1, 10    
                hostArray(i) = 0  
        end do  
        call writeIndex(hostArray, 10)
        do i = 1, 10    
                if ( hostArray(i) /= i ) then
                        errors = errors + 1    
                end if  
        end do  
        if ( errors /= 0 ) then
                stop 1  
        end if
        print*, "======= FORTRAN Test passed! ======="
!       stop 0
end program main
subroutine writeIndex(int_array, array_length)
        use test_0
        integer :: int_array(*)
        integer :: array_length  
        integer :: new_len
        integer :: v = 5 ! global, when given value like this
        ! v = 5 ! local, when given value like this 
!$omp target map(tofrom:new_len) map(tofrom:decltar_v)
        new_len = decltar_v + v 
!$omp end target
        print*, new_len
        do index_ = 1, new_len 
                int_array(index_) = index_  
        end do
end subroutine writeIndex

This is by no means full support for map for either implicit, explicit or declare target, but it will hopefully allow a bit more support for them when applied.

I intend to look into the declare target component for the time being, however, I will come back to the other argument types if no one gets around to them before I finish the declare target work! If someone wishes to build on or get some ideas of where to start with the map clauses to further increase support I can give a breakdown. However, if building on this patch, it likely needs the following extensions to start:

  1. An MLIR attribute for capture types on map clauses, in addition to the from/to/fromto, e.g. capture bythis, byref, byval
  2. A lowering to this capture type from the PFT by the frontend during Target Op lowering, which needs to create a mapping from the type and method of it's capture to the capture type, similarly to the way Clang currently does it
  3. An extension to the current optimsation pass (or better yet, move this pass to the PFT lowering so it's not a pass anymore and we have access to all the PFT details) to give all the implicit captures an appropriate capture type as they are hoisted into the map
  4. Further extension of the Target Op Map PFT lowering to support more maptypes, a lot of the maptypes generated are very basic and will cause runtime errors at the moment, as maptypes are a conglomerate of a bunch of bitshifts based on the type of the variable, it's capture and how
  5. Extension or some variation of the castInput function (perhaps a rename as well) to mimic GenerateOpenMPCapturedVars from Clang which generates additional operations for inputs based on capture type (operations for how to appropriately access things byval/byref etc.), an extension to the additional version for host which I have currently called createAlteredByCaptureMap which generates some additional operations when passing input to kernels via the map.

This is just some possible things that need done if using this patch as a base, but will generally give an idea of the extent of things that need doing for implicit/explicit map variables. And of course, anyone picking this up can do as they wish/best see fit these are suggestions from what I found doing this patch!

The following two Clang functions may be of interest when doing the above (although, not exhaustive, anywhere where the maptype enum or capture enum are referenced is of interest, in particular one really large map related function):

  • GenerateOpenMPCapturedVars (whole function)
  • emitOutlinedFunctionPrologue (primarily the end of the function dealing with input)

There's essentially a lot of special case operation generation for argument passing and accessing based on capture type inside of Clang.

There's a lot of TODO/FIXME's all over the place at the moment and some dump/outs, so please be aware if you directly apply the entire patch.

The patch also looks a little beefier than it should be as it applies some other phab patches I have to work, namely D149162 and D149368, the former is now upstreamed, so updating ATD on main will shrink the diff a little.

@agozillon agozillon force-pushed the amd-trunk-dev-map-v2 branch from 98b6ffb to 38aabd4 Compare August 22, 2023 16:33
@github-actions
Copy link

github-actions bot commented Sep 27, 2023

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

You can test this locally with the following command:
git-clang-format --diff 911367e4fd735f70ffcc3ff8c8540bff92d47068 90e1b5b82e080560269c98b052b597577b98593f -- flang/include/flang/Lower/OpenMP.h flang/lib/Frontend/FrontendActions.cpp flang/lib/Lower/Bridge.cpp flang/lib/Lower/ConvertVariable.cpp flang/lib/Lower/OpenMP.cpp flang/lib/Optimizer/Builder/FIRBuilder.cpp flang/lib/Optimizer/CodeGen/CodeGen.cpp llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
View the diff from clang-format here.
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index e54d5f0380c6..ec6bac3ff860 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -2925,7 +2925,7 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
   }
 
   ompBuilder->CurrentTargetInfo.emplace();
-  
+
   builder.restoreIP(ompBuilder->createTarget(
       ompLoc, targetOp.isTargetSPMDLoop(), allocaIP, builder.saveIP(),
       entryInfo, kernelInput, genMapInfoCB, bodyCB, argAccessorCB));

@agozillon agozillon force-pushed the amd-trunk-dev-map-v2 branch 4 times, most recently from 6521275 to ddb01b1 Compare September 29, 2023 15:40
@agozillon agozillon force-pushed the amd-trunk-dev-map-v2 branch from b11ccfb to 710dc8e Compare October 14, 2023 02:18
@agozillon agozillon force-pushed the amd-trunk-dev-map-v2 branch 2 times, most recently from 6404164 to 97579af Compare October 25, 2023 04:50
@agozillon agozillon force-pushed the amd-trunk-dev-map-v2 branch from 90e1b5b to d282db4 Compare November 6, 2023 16:36
Consists of:
   1-D array sectioning mapping
   initial declare target mapping
   initial 1-D pointer/alloca/target mapping
   A start at structure lowering with contained pointers (can't test yet due to some required frontned work, but it's utilised to lower pointers etc. at the moment, which is similar in Fortran)
   tidying up of the above
   Sergio's CSE patch
@agozillon agozillon force-pushed the amd-trunk-dev-map-v2 branch from d282db4 to f741bbb Compare November 6, 2023 16:41
…IFA PR) to support target loops

Handling of implicits for target parallel do/parallel distribtue do, to test a larger subset of milestone 3a/3b.

This will be deprecated in favour of IFA so not a lot of
effort put into removing
obvious code duplication.
@agozillon agozillon changed the title [WIP][MLIR][Flang] WIP patch for adding some support for map and declare target on target op [WIP][MLIR][Flang] WIP additional mapping support Nov 14, 2023
@agozillon
Copy link
Contributor Author

Now superseded by: #249

Which is the allocatables segment of this PR with the implicit handling stripped out to utilise the new IFA approach for implicits.

@agozillon agozillon closed this Nov 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant