This is an archive of the discontinued LLVM Phabricator instance.

[mlir] add RemoveConstantIfCondition to populateOpenACCToSCFConversionPatterns
ClosedPublic

Authored by python3kgae on Jan 21 2023, 8:38 AM.

Details

Summary

For issue https://github.com/llvm/llvm-project/issues/60058
It hit assert when legalizePatternResult on success of ExpandIfCondition which did nothing just return success when if condition is constant.

Added RemoveConstantIfCondition to remove the if cond by getCanonicalizationPatterns.
Also remove the check for constant if cond in ExpandIfCondition and
change check ifCond to assert because only op with ifCond will need legalize in ConvertOpenACCToSCFPass

Diff Detail

Event Timeline

python3kgae created this revision.Jan 21 2023, 8:38 AM
python3kgae requested review of this revision.Jan 21 2023, 8:38 AM
mehdi_amini accepted this revision.Jan 21 2023, 9:03 AM

Nit: if you refer to a GitHub issue as Fixes #60058 in the commit message, it'll automatically close the issue when you push.

This revision is now accepted and ready to land.Jan 21 2023, 9:03 AM

Why creating an op if we know we don't need it?

clementval added inline comments.Jan 21 2023, 9:25 AM
mlir/lib/Conversion/OpenACCToSCF/OpenACCToSCF.cpp
38

ifCond is an optional so if it's not present we should do nothing. The assert is not checking the same things. If the op has no ifCond it will just not compile.

clementval added inline comments.Jan 21 2023, 9:34 AM
mlir/lib/Conversion/OpenACCToSCF/OpenACCToSCF.cpp
57

This will probably trigger the assert when ifCond is absent.

python3kgae marked 2 inline comments as done.Jan 21 2023, 11:19 AM

Why creating an op if we know we don't need it?

ExpandIfCondition should not cause crash with or without RemoveConstantIfCondition.
When RemoveConstantIfCondition is not there, return success but do nothing will cause assert fail in OperationLegalizer::legalizePatternResult.

assert((replacedRoot() || updatedRootInPlace()) &&
       "expected pattern to replace the root operation");
mlir/lib/Conversion/OpenACCToSCF/OpenACCToSCF.cpp
38

It is optional, but ExpandIfCondition only hit on Ops with ifCond.

57

It is controlled by

target.addDynamicallyLegalOp<acc::ExitDataOp>(
    [](acc::ExitDataOp op) { return !op.getIfCond(); });

in
https://github.com/llvm/llvm-project/blob/main/mlir/lib/Conversion/OpenACCToSCF/OpenACCToSCF.cpp#L85

Only Ops which has ifCond need to be legalized.

Could just inverse the condition and return failure()?

mehdi_amini requested changes to this revision.Jan 21 2023, 11:22 AM
This revision now requires changes to proceed.Jan 21 2023, 11:22 AM
python3kgae marked 2 inline comments as done.Jan 21 2023, 12:47 PM

Could just inverse the condition and return failure()?

Not sure which path you're talking about :(

change assert(op.getIfCond() && "expected Op with IfCond"); to

if (!op.getIfCond())
  return failure();

or
add back the check ifCond as constant like
if (op.getIfCond().template getDefiningOp<arith::ConstantOp>())

return failure();

Or both?

Could just inverse the condition and return failure()?

Not sure which path you're talking about :(

change assert(op.getIfCond() && "expected Op with IfCond"); to

if (!op.getIfCond())
  return failure();

or
add back the check ifCond as constant like
if (op.getIfCond().template getDefiningOp<arith::ConstantOp>())

return failure();

Or both?

For if (op.getIfCond().template getDefiningOp<arith::ConstantOp>())
maybe we could just copy code of RemoveConstantIfCondition instead of return failure() and remove

 acc::EnterDataOp::getCanonicalizationPatterns(patterns,
                                              patterns.getContext());
acc::ExitDataOp::getCanonicalizationPatterns(patterns, patterns.getContext());
acc::UpdateOp::getCanonicalizationPatterns(patterns, patterns.getContext());

in current change?

Could just inverse the condition and return failure()?

Not sure which path you're talking about :(

change assert(op.getIfCond() && "expected Op with IfCond"); to

if (!op.getIfCond())
  return failure();

Yeah I wonder if this wouldn't just be enough for the crash here?

if (op.getIfCond().template getDefiningOp<arith::ConstantOp>())

return failure();

Or both?

This is also a possibility to have both, I assume that we didn't want to build an if that would be folded away?

Do RemoveConstantIfCondition in ExpandIfCondition for constant if cond.

mehdi_amini accepted this revision.Jan 23 2023, 1:41 AM
This revision is now accepted and ready to land.Jan 23 2023, 1:41 AM