llvm / llvm/llvm-project

[RISCV] Failure to merge memset (with gaps) to alloca

Open
#171,473 1 comment 0 reactions 0 assignees View on GitHub
backend:RISC-V missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Motivating example: https://godbolt.org/z/efv64oxoT

This is reduced from something in SPEC's perlbench regmatch, but was tweaked enough during reduction to likely be no longer representative. I thought the result was interesting on it's own.

If we have a stack allocation with multiple memsets used to initialize subsets of it, we can merge the memsets into one larger memset (by writing zeros to otherwise uninitialized space in the alloca). In this particular case, this would let us use a single VL predicated m8 store.

Specifically for RISCV, we also have the possibility of using a *masked* vse64.v if desired. That could be used to avoid the need to write to a memory location which wasn't otherwise written to, and thus could generalize the transform.

I think this hints at some RISCV specific opportunities for store merging in DAGCombine (or possibly elsewhere). Figuring out the costing of the masking vs multiple stores is unfortunately an interesting question.

Contributor guide

Open the contributing guide

Research direction

Start with the reduced Godbolt example and the RISC-V DAGCombine area mentioned in the issue. Investigate how multiple memset operations initialize disjoint portions of an alloca, then compare merging them into a wider store with using a masked vse64.v. Done means establishing an implementation direction and costing behavior for the proposed optimization.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.