This is an archive of the discontinued LLVM Phabricator instance.

[Flang][MLIR][OpenMP] Add support for simdlen clause
ClosedPublic

Authored by psoni2628 on Jul 20 2022, 12:13 PM.

Details

Summary

This supports lowering from parse-tree to MLIR and translation from
MLIR to LLVM IR using OMPIRBuilder for OpenMP simdlen clause in SIMD
construct.

Diff Detail

Event Timeline

psoni2628 created this revision.Jul 20 2022, 12:13 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJul 20 2022, 12:13 PM
psoni2628 requested review of this revision.Jul 20 2022, 12:13 PM

Thanks for working on this patch. Can we add a verifier for
simdlen to error when the given value is negative? From the OpenMP standards "The parameter of the simdlen clause must be a constant positive integer expression."

psoni2628 added a comment.EditedJul 20 2022, 2:17 PM

Thanks for working on this patch. Can we add a verifier for
simdlen to error when the given value is negative? From the OpenMP standards "The parameter of the simdlen clause must be a constant positive integer expression."

Good point. I think it would be simpler to change simdlen argument type from I32 to Confined<OptionalAttr<I64Attr>, [IntMinValue<0>]> in the MLIR op definition. We use the aforementioned type currently for clauses such as ordered, which are also required to be "constant positive integer expression".

psoni2628 updated this revision to Diff 446311.Jul 20 2022, 5:45 PM
  • Use IntPositive to ensure that simdlen is a constant, positive integer
arnamoy10 requested changes to this revision.Jul 20 2022, 6:45 PM
  1. Can we have a test case in invalid.mlir
  2. Can we have a testcase where the simdlen is specified through a function argument or a local variable?
This revision now requires changes to proceed.Jul 20 2022, 6:45 PM
psoni2628 updated this revision to Diff 447737.Jul 26 2022, 9:11 AM
  • Add invalid.mlir testcase
  • Add Flang test case with simdlen from a local parameter instead of a literal
  • Rebase so that the patch can be applied
shraiysh added inline comments.Jul 27 2022, 4:28 AM
flang/lib/Lower/OpenMP.cpp
907–909
1049–1050

We can have simdlenClauseOperand as an IntegerAttr instead of an Attribute (suggested edit in the code above). That will avoid type-casting to and from Attribute for this.

flang/test/Lower/OpenMP/simd.f90
61

Can we also have a test here that has an expression. Like SIMDLEN(simdlen * 2 + n) or something.

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
975

nit: expand auto.

978

There are no tests for MLIR->LLVM IR translation. Changes to OpenMPToLLVMIRTranslation.cpp and the tests for that (in mlir/test/Target/LLVMIR/openmp-llvm.mlir) should be a part of the same patch. You can send them as a separate patch if you'd like.

psoni2628 updated this revision to Diff 448042.Jul 27 2022, 7:49 AM
  • Expand auto
  • Add expression Flang test case
  • Use IntegerAttr directly to avoid the need for a dyn_cast
  • Add mlir/test/Target/LLVMIR/openmp-llvm.mlir testcase
flang/test/Lower/OpenMP/simd.f90
61

I instead used SIMDLEN(simdlen*2 + 2), because in this example n is not constant, so you can't use it in an expression for simdlen.

shraiysh accepted this revision.Jul 27 2022, 8:09 AM

LGTM.

flang/test/Lower/OpenMP/simd.f90
61

Hmm, gfortran seems to accept simdlen*2+n in this example. The standard mentions that the argument must be a "constant positive integer expression". It is probably incorrect behavior in gfortran. Please wait for a day or two if anyone has a preference for being accurate to standard vs being compatible with gfortran. If no one objects, we can go ahead and merge this. For me, this is fine.

psoni2628 added inline comments.Jul 27 2022, 8:49 AM
flang/test/Lower/OpenMP/simd.f90
61

I wanted to point out that it is the parser that complains with Must be a constant value. If we are going to try and be compatible with gfortran, I think it would make more sense to do this in a separate patch where we change the parser and the other affected parts accordingly.

Also, if you write a similar C program as shown below and compile it with Clang, Clang also complains similarly, so I believe Flang's existing parsing behaviour is correct.

#define N  1000000000

void func (int n) {

	int a[N] = {0};

	#pragma omp simd safelen(n)
	for (int i=0; i<N; i++) {
		a[i] = a[i] + i;
	}
}
./ex.c:10:27: error: expression is not an integer constant expression
        #pragma omp simd safelen(n)
peixin accepted this revision.Jul 28 2022, 6:06 AM

Nit: In summary:

This supports lowering from parse-tree to MLIR and translation from MLIR to LLVM IR using OMPIRBuilder for OpenMP simdlen clause in SIMD construct.

flang/test/Lower/OpenMP/simd.f90
61

@shraiysh @psoni2628 Don't worry about this. It's safe to emit one semantic error for this. Check the following cases:

subroutine sub(N, x, y, z)
  integer :: N
  integer :: l = 1
  integer :: x(N), y(N), z(N)
  !$omp simd simdlen(l)
  do i = 1, N
    x(i) = x(i) + y(i)*z(i)
  enddo
  !$omp end simd
end
subroutine sub(N, x, y, z)
  integer :: N
  integer, parameter :: l = 1
  integer :: x(N), y(N), z(N)
  !$omp simd simdlen(l)
  do i = 1, N
    x(i) = x(i) + y(i)*z(i)
  enddo
  !$omp end simd
end

Run gfortran -fopenmp test.f90 -O3 -S -o test-omp.s and you will see simdlen with variable won't work.

The variable is unknown in compile-time. I don't think you can add one variable in the loop vectorization metadata.

psoni2628 edited the summary of this revision. (Show Details)Jul 28 2022, 6:45 AM
psoni2628 marked 7 inline comments as done.
This revision was not accepted when it landed; it landed in state Needs Review.Jul 28 2022, 8:49 AM
This revision was automatically updated to reflect the committed changes.