isHardwareLoopProfitable() called with empty assumption cache in hwloops pass

Hey all,

It seems that when HardwareLoops calls the isHardwareLoopProfitable TTI hook, it never has a populated AssumptionCache. Some debugging revealed that HardwareLoops runs during the PreISel phase for ARM and PPC. However, the CodeGenPrepare pass runs before PreISel and removes all assumes meaning that the AssumptionCache in HardwareLoops will end up empty.

From what I gather (and let me know if I’m wrong), only PPC uses the AssumptionCache in isHardwareLoopProfitable but only to aid in some cost analysis. I was wondering whether it’s intended behaviour to have an empty AssumptionCache during HardwareLoops? I ask because I was looking into using assumes to persuade HardwareLoops into emitting hardware intrinsics for our (downstream) target.

Kind regards,

Janek van Oirschot

I can’t imagine that being the intended behaviour. I don’t think we have paid much attention to the assumption cache in the ARM implementation. Some parts of the hardware loop infrastructure were factored out from the initial PPC implementation, which I think explains it is there and used by PPC, but not in the ARM implementation. But perhaps Sam knows more.

I have never looked into the AssumptionCache, but I assume there’s way to retrigger and repopulate it (after CGP)?

Just out of curiousity, can you perhaps tell more how you would like to persuade/force hardware loops with an assume? There are some options at the moment (but would apply to all loops in the compilation unit), don’t think we e.g. looked into a pragma, so it sounds interesting. I guess this is a hint about the iteration count?

Cheers,
Sjoerd.

Indeed, it’s just there because the original PPC implementation used it. Looking back through the commits, I didn’t move the pass into a different phase so either PPC has either never had a populated assumption cache or never noticed the change when it was cleared.

As Sjoerd said, can we re-populate it?

As long as it runs after LSR, I can’t immediately think of anything that would affect the Arm implementation (famous last words!) if we moved the transform a bit earlier in the pipeline.

Regards,
Sam

The usage of AssumptionCache in PPC hardware loop was introduced in 0724fea2da637883f1461e12ff46d596a816f758

For PPC hardware loop pass, we want to make sure we will not convert some small loops into hardware loop. Small loop is identified by instruction number of the loop. So we used CodeMetrics class to calculate the instruction number.

And in the CodeMetrics class, when it calculates the instruction number, it will exclude the ephemeral values(llvm.assume related instructions) first, that’s why we need assumption cache analysis.

I think it is ok to have empty AssumptionCachein PPC hardware loop pass as it is true that after CGP there is no ephemeral values any more.

But maybe it makes more sense to have other APIs for CodeMetrics class to calculate instruction number without AssumptionCache.

Thanks.

BRS//
Chen Zheng
Power Compiler Backend Developer

graycol.gifSam Parker —2021/03/25 05:44:25 PM—Indeed, it’s just there because the original PPC implementation used it. Looking back through the co

Just out of curiousity, can you perhaps tell more how you would like to persuade/force hardware loops with an assume? There are some options at the moment (but would apply to all loops in the compilation unit), don’t think we e.g. looked into a pragma, so it sounds interesting. I guess this is a hint about the iteration count?

Yeah, basically hint about the iteration count so the user can emit hardware loops on a per-loop basis. We were also thinking about using pragmas (and I believe it would be a more user friendly way to emit hardware loops) but I noticed that I had most tools at hand (assumptioncache, computeKnownBits) to possibly get it working with assume intrinsics as an “easy” first step.

As Sjoerd said, can we re-populate it?

I believe that after CGP all assume intrinsics will be gone so I don’t think that will be possible (unless there’s another way to find the assumptions and repopulate the assumptioncache). Moving the hardwareloops pass before CGP sounds like a possibility but I’m not sure about the impact of doing so (in terms of no. of hwloops emitted).

And in the CodeMetrics class, when it calculates the instruction number, it will exclude the ephemeral values(llvm.assume related instructions) first, that’s why we need assumption cache analysis.

I did notice that isHardwareLoopProfitable is also called from TTI’s canSaveCmp in PPC which may explain the need of using the assumptioncache in isHardwareLoopProfitable.

Kind regards,

Janek van Oirschot

image001.gif