[all-commits] [llvm/llvm-project] 80ced5: [SPIRV] Emit intrinsics for globals only in functi...

Juan Manuel Martinez Caamaño via All-commits all-commits at lists.llvm.org
Mon Feb 2 04:34:43 PST 2026


  Branch: refs/heads/users/jmmartinez/spirv/memory_consumption
  Home:   https://github.com/llvm/llvm-project
  Commit: 80ced5c383db51837d9479773ceb62761cfcff3b
      https://github.com/llvm/llvm-project/commit/80ced5c383db51837d9479773ceb62761cfcff3b
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
    M llvm/test/CodeGen/SPIRV/extensions/SPV_KHR_float_controls2/exec_mode3.ll
    M llvm/test/CodeGen/SPIRV/extensions/SPV_NV_shader_atomic_fp16_vector/atomicrmw_faddfsub_vec_float16.ll
    M llvm/test/CodeGen/SPIRV/extensions/SPV_NV_shader_atomic_fp16_vector/atomicrmw_fminfmax_vec_float16.ll
    M llvm/test/CodeGen/SPIRV/pointers/fun-with-aggregate-arg-in-const-init.ll

  Log Message:
  -----------
  [SPIRV] Emit intrinsics for globals only in function that references them

In the SPIRV backend, the SPIRVEmitIntrinscs::processGlobalValue
function adds intrinsic calls for every global variable of the module,
on every function.

These intrinsics are used to keep track of global variables, their types and
initializers.

In SPIRV everything is an instruction (even globals/constants). We currently
represent these global entities as individual instructions on every function.
Later, the `SPIRVModuleAnalysis` collects these entities and maps function _local_ registers
to _global_ registers. The `SPIRVAsmPrinter` is in charge of mapping back the _local_
registers to the appropiate _global_ register.

These instructions associated with global entities on functions that do not reference them leads
to a bloated intermediate representation and high memory consumption (as it happend
in https://github.com/llvm/llvm-project/issues/170339).

Consider this example:

```cpp
  int A[1024] = { 0, 1, 2, ..., 1023 };
  int get_a(int i) {
    return A[i];
  }

  void say_hi() {
    puts("hi!\n");
  }
```

Although, `say_hi` does not reference `A`; we would generate machine
instructions for the declaration of `A`, for the constant array that
initializes it, and for the 1024 constant elements of `A`.

This patch doesn't fix the underlying issue, but it mitigates it by only
emitting global-variable SPIRV intrinsics on the functions that use it.
If the global is not referenced by any function, we just pick the first
function definition.

With this patch, the example in https://github.com/llvm/llvm-project/issues/170339 drops from
~33Gb of maximum resident set size to less than ~570Mb. And compile
time goes from 2:09min to 1:26min.


  Commit: fd47c99c1d05c9c84caf9870ab424ffc5fc5bb7a
      https://github.com/llvm/llvm-project/commit/fd47c99c1d05c9c84caf9870ab424ffc5fc5bb7a
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] Compute which functions reach which globals only once


  Commit: ab7b0f7e3f4cbccd7abdc044cbd0260ee7d86c48
      https://github.com/llvm/llvm-project/commit/ab7b0f7e3f4cbccd7abdc044cbd0260ee7d86c48
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] Address typo in .clear() and me being lazy and not doing a .find()


  Commit: bfc0e2eda04bf07f0fb516963b56f03ab32f3c3e
      https://github.com/llvm/llvm-project/commit/bfc0e2eda04bf07f0fb516963b56f03ab32f3c3e
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] Move loop body to helper method


  Commit: 4f3bf04eb54bc177b1ddca4726ad8a506aaaba05
      https://github.com/llvm/llvm-project/commit/4f3bf04eb54bc177b1ddca4726ad8a506aaaba05
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] move second loop to helper method


  Commit: b29192d16cdd5139e21dd08484ad934e0289a1c9
      https://github.com/llvm/llvm-project/commit/b29192d16cdd5139e21dd08484ad934e0289a1c9
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] found -> reached


  Commit: 6461b82b5be549e82d43fcd66d98a1ae188cc54f
      https://github.com/llvm/llvm-project/commit/6461b82b5be549e82d43fcd66d98a1ae188cc54f
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] miscleaneaous renaming


  Commit: 2f3419c0b4490579a6f14e95f7cc42419f65aace
      https://github.com/llvm/llvm-project/commit/2f3419c0b4490579a6f14e95f7cc42419f65aace
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] Add const


  Commit: 9c7e6c8748a968c5de80af4a10512be3008fb63b
      https://github.com/llvm/llvm-project/commit/9c7e6c8748a968c5de80af4a10512be3008fb63b
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] reuse vector


  Commit: e6ce1a2728000bb209f04e90765fd2532fe3794b
      https://github.com/llvm/llvm-project/commit/e6ce1a2728000bb209f04e90765fd2532fe3794b
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] add const and rename


  Commit: af0668e1e43a5ee655c2c2849373299eabc35f17
      https://github.com/llvm/llvm-project/commit/af0668e1e43a5ee655c2c2849373299eabc35f17
  Author: Juan Manuel Martinez Caamaño <jmartinezcaamao at gmail.com>
  Date:   2026-02-02 (Mon, 02 Feb 2026)

  Changed paths:
    M llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

  Log Message:
  -----------
  [Review] Avoid repeating the type


Compare: https://github.com/llvm/llvm-project/compare/3d01fff734f4...af0668e1e43a

To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list