Always-inliner cost grows with call depth: a large body is re-cloned at every single-call wrapper level

Open
#9,075 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
12/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp, python

Research direction

The ask is an algorithmic change to the always-inliner in LLVM's inlining pass as used by DXC, so start by finding where DXC drives the inliner and where CloneAndPruneIntoFromInst/fixupLineNumbers/alias-scope metadata costs come from in the LLVM sources vendored in this repo. Build dxc, generate d0.hlsl/d4.hlsl/d8.hlsl with the provided gen_hlsl.py, and compare -ftime-report for Inliner for always_inline functions across depths to establish the baseline. Done means the single-call-site case stops re-cloning the body at each wrapper level and the depth-4/depth-8 inliner times approach the depth-0 time without changing emitted DXIL.

Written by the indexing model from the issue text.

Description

Summary

Inliner for always_inline functions dominates compile time for large compute shaders (49-75% of passes;
-ftime-report). -ftime-trace shows the cost is CloneAndPruneIntoFromInst + fixupLineNumbers + alias-scope
metadata for each inlining of an already-large function. Because inlining is bottom-up, a body under N
single-call wrappers is cloned N+1 times. Generated HLSL (Tint, Slang) commonly has 2-4 such levels (kernel
function -> entry _inner shim -> entry).

Measurements (dxcompiler 1.9.2609 from Chrome, -T cs_6_6 /O3 /Gis /Zpr -HV 2018)

Synthetic (generated as Slang -> WGSL -> Tint HLSL in our measurement; the same DXIL in every case): depth 0: 5.6-6.0 s; depth 4: 9.2-9.6 s;
depth 8: 11.5-12.6 s. The body inlined into the entry by hand: 4.2-5.0 s.
Production kernel (1 MB HLSL, 247 KB DXIL): 15.2 s of passes, 7.5 s inliner. Three full-body clones at
0.73-0.82 s each in CloneAndPrune plus ~0.7 s each in metadata/line fixups. Hand-flattening two levels: 15.6 ->
12.3 s.

Ask

Inline a function with a single call site by moving its body, not cloning it (the callee is deleted
afterwards anyway). Or process single-caller chains top-down, so the large body is copied once.

Repro

The script below writes plain HLSL with the same structure (a 1200-block body under DEPTH single-call wrappers):

python gen_hlsl.py 0 d0.hlsl & python gen_hlsl.py 4 d4.hlsl & python gen_hlsl.py 8 d8.hlsl
dxc -T cs_6_6 -E main /O3 /Gis /Zpr -HV 2018 -ftime-report d8.hlsl

Compare the Inliner for always_inline functions time across depths.

gen_hlsl.py
import sys
# gen_hlsl.py DEPTH OUT.hlsl: a large compute body under DEPTH single-call wrapper functions.
depth, out = int(sys.argv[1]), sys.argv[2]
s = ["RWStructuredBuffer<float4> buf : register(u0);",
     "float4 h(float4 a, float4 b, uint k) { if ((k & 3u) == 0u) return a * b + 0.5; return a.yzwx - b * 0.25; }",
     "float4 body(uint t) {", "  float4 a = buf[t]; float4 b = buf[t + 1u];"]
for i in range(1200):
    s.append(f"  if (a.x > {i * 0.001:.3f}) {{ a = h(a, b, {i}u + t); b = h(b, a, t ^ {i}u); }} else {{ b = h(a, b, t + {i}u); }}")
s += ["  return a + b;", "}"]
prev = "body"
for d in range(depth):
    s.append(f"float4 w{d}(uint t) {{ return {prev}(t) * 1.0001; }}")
    prev = f"w{d}"
s.append(f"[numthreads(64, 1, 1)] void main(uint3 id : SV_DispatchThreadID) {{ buf[id.x] = {prev}(id.x); }}")
open(out, "w").write("\n".join(s) + "\n")
Dominant language
C++
Stars
3.7k
Forks
926
Avg merge
3d 22h
Merged PRs (30d)
45

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/DirectXShaderCompiler

All issues in microsoft/DirectXShaderCompiler

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.