This is an archive of the discontinued LLVM Phabricator instance.

[LoopVectorize] Propagate fast-math flags for VPInstruction
ClosedPublic

Authored by RosieSumpter on Nov 3 2021, 10:27 AM.

Details

Summary

In-loop vector reductions which use the llvm.fmuladd intrinsic involve
the creation of two recipes; a VPReductionRecipe for the fadd and a
VPInstruction for the fmul. If the call to llvm.fmuladd has fast-math flags
these should be propagated through to the fmul instruction, so an
interface setFastMathFlags has been added to the VPInstruction class to
enable this.

Depends on D111555

Diff Detail

Event Timeline

RosieSumpter created this revision.Nov 3 2021, 10:27 AM
RosieSumpter requested review of this revision.Nov 3 2021, 10:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 3 2021, 10:27 AM

What happens when the instruction is printed inside VPInstruction::print? Can (should?) we get it to display the fast math flags?

What happens when the instruction is printed inside VPInstruction::print? Can (should?) we get it to display the fast math flags?

Yeah, I think the flags should be handled in ::print and it would be good to also add a test in llvm/test/Transforms/LoopVectorize/vplan-printing.ll

llvm/lib/Transforms/Vectorize/VPlan.h
881

can we assert that this is only used for opcodes that can take fast-math flags? On the IR side this is done by checking for FPMathOperator, but there's no helper to check if an opcode is a FP operation. Perhaps it would be sufficient to check for the relevant opcodes?

Also, for now we only need to set fast-math flags at construction time I think, so could this be set directly in the constructor?

  • Added a print method to the FastMathFlags class and used it in VPInstruction::print
  • Added a test to vplan-printing.ll
  • Assert that the VPInstruction opcode is for a floating-point operation when setting fast-math flags

Thanks for the comments @dmgreen and @fhahn. RE printing the fast-math flags, it seems the only way is to check for each flag and print the correct word, as was being done in WriteOptimizationInfo in llvm/lib/IR/AsmWriter.cpp. Since this is now occurring in two places, I've added a print method to FastMathFlags. Let me know if you think this is the right way to go.

llvm/lib/Transforms/Vectorize/VPlan.h
881

Hey @fhahn, thanks for this, I've added an assert to check for all the fp opcodes in setFastMathFlags. I haven't set the fast-math flags directly in the constructor for now since the setFastMathFlags approach makes the flag setting explicit, and mirrors the Instruction class. As always though, I'm happy to change it if you still think setting the flags in the constructor is the best way.

sdesmalen added inline comments.Nov 8 2021, 6:11 AM
llvm/lib/IR/AsmWriter.cpp
1312

Hi @RosieSumpter, could you maybe also define an overloaded operator<< for FastMathFlags, so that you can write Out << FPO->getFastMathFlags()?

  • Defined overloaded operator << for FastMathFlags and used it in AsmWriter.cpp
sdesmalen added inline comments.Nov 9 2021, 4:52 AM
llvm/lib/Transforms/Vectorize/VPlan.cpp
765

nit: O << FMF ?

llvm/test/Transforms/LoopVectorize/vplan-printing.ll
256

nit: Should VPReductionRecipe::print also print the fast-math flags?

  • Changed FMF.print(O) to O << FMF
  • Added fast-math flags to VPReductionRecipe::print
RosieSumpter marked 2 inline comments as done.Nov 10 2021, 1:19 AM
sdesmalen accepted this revision.Nov 10 2021, 1:36 AM

Thanks for the changes @RosieSumpter, LGTM!

This revision is now accepted and ready to land.Nov 10 2021, 1:36 AM
fhahn added inline comments.Nov 16 2021, 3:14 AM
llvm/lib/Transforms/Vectorize/VPlan.cpp
1215

That change is not really related to the patch title/description. I think it would be good to either adjust the title or commit it separately.

llvm/lib/Transforms/Vectorize/VPlan.h
881

Thanks for adding the assert! For VPInstructions we do not need the full flexibility provided by setFastMathFlags, as for now we only setting them at construction time. This was the main reason to suggest doing it directly in the constructor. But I guess in the future this may be helpful in case we need to drop fast-math flags.

This revision was landed with ongoing or failed builds.Nov 24 2021, 1:00 AM
This revision was automatically updated to reflect the committed changes.
RosieSumpter marked an inline comment as done and an inline comment as not done.Nov 24 2021, 1:02 AM
RosieSumpter added inline comments.
llvm/lib/Transforms/Vectorize/VPlan.cpp
1215

Hi @fhahn, agreed - I split off this change into a separate commit before merging.