Skip to content

如何获取编译后的kernel的sgpr,vgpr使用量以及使用的shared memory大小 #196

Description

@xiaoyu1004

如何获取编译后的kernel的sgpr,vgpr使用量以及使用的shared memory大小呢,driver驱动device的话需要这些信息,编译器没有把这些信息写进最终的elf文件吗

Activity

  1. changed the title [-]kernel使用的sgpr, vgpr,shared memory大小没有写进elf文件吗[/-] [+]如何获取编译后的kernel的sgpr,vgpr使用量以及使用的shared memory大小[/+] on Aug 26, 2025
  2. Humber-186 commented on Dec 21, 2025

    @Humber-186
    Collaborator

    以下信息并非来自第一手开发者,只是文档中的少量描述 + AI代码分析


    有的,readelf可以看到:

    $ ./install/bin/llvm-readelf -S rodinia/opencl/gaussian/object0.riscv
    There are 15 section headers, starting at offset 0x6420:
    
    Section Headers:
      [Nr] Name              Type            Address  Off    Size   ES Flg Lk Inf Al
      [ 0]                   NULL            00000000 000000 000000 00      0   0  0
      [ 1] .text             PROGBITS        80000000 001000 000368 00  AX  0   0  4
      [ 2] .tohost           PROGBITS        80001000 002000 000048 00  WA  0   0 64
      [ 3] .eh_frame_hdr     PROGBITS        80002000 003000 000000 00  WA  0   0  1
      [ 4] .eh_frame         PROGBITS        80002000 003000 000018 00   A  0   0  4
      [ 5] .data             PROGBITS        80003000 004000 000008 00  WA  0   0  1
      [ 6] .sdata            PROGBITS        80003008 004008 000000 00  WA  0   0  1
      [ 7] .bss              NOBITS          80003008 004008 000000 00  WA  0   0  1
      [ 8] .comment          PROGBITS        00000000 004008 0000f4 01  MS  0   0  1
      [ 9] .ventus.resource.Fan1 PROGBITS    00000000 0040fc 000008 00   W  0   0  1        <= Note this
      [10] .ventus.resource.Fan2 PROGBITS    00000000 004104 000008 00   W  0   0  1        <= Note this
      [11] .riscv.attributes RISCV_ATTRIBUTES 00000000 00410c 00005f 00      0   0  1
      [12] .symtab           SYMTAB          00000000 00416c 001380 10     14 289  4
      [13] .shstrtab         STRTAB          00000000 0054ec 00009a 00      0   0  1
      [14] .strtab           STRTAB          00000000 005586 000e9a 00      0   0  1
    Key to Flags:
      W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
      L (link order), O (extra OS processing required), G (group), T (TLS),
      C (compressed), x (unknown), o (OS specific), E (exclude),
      R (retain), p (processor specific)

    一个ELF中可能有多个kernel(上边的有Fan1, Fan2两个),每个kernel都有一个section名为.ventus.resource.<kernelname>记录自己的资源用量信息

    .ventus.resource.<kernelname> section的尺寸固定为8字节,实际上是4个int16(unsinged?)。生成这个section的代码在llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp::RISCVAsmPrinter::runOnMachineFunction

      if (MF.getInfo<RISCVMachineFunctionInfo>()->isEntryFunction()) {
        MCSectionELF *ResourceSection = OutContext.getELFSection(
            ".ventus.resource." + MF.getName(), ELF::SHT_PROGBITS, ELF::SHF_WRITE);
        OutStreamer->switchSection(ResourceSection);
        OutStreamer->emitInt16(
                        CurrentProgramInfo->SubProgramInfoVec[FuncCount].VGPRUsage);
        OutStreamer->emitInt16(
                        CurrentProgramInfo->SubProgramInfoVec[FuncCount].SGPRUsage);
        OutStreamer->emitInt16(
                        CurrentProgramInfo->SubProgramInfoVec[FuncCount].LDSMemory);
        OutStreamer->emitInt16(
                        CurrentProgramInfo->SubProgramInfoVec[FuncCount].PDSMemory);
      }

    例如下边的ELF:

    $ ./install/bin/llvm-readelf -x .ventus.resource.matadd pocl/build/examples/matadd/object0.riscv
    Hex dump of section '.ventus.resource.matadd':
    0x00000000 05000700 10000800

    0x00000000是段偏移,其后的8字节应该是小端序的int16[]即:

    • 05 00 → 0x0005 → VGPRUsage = 5
    • 07 00 → 0x0007 → SGPRUsage = 7
    • 10 00 → 0x0010 → LDSMemory = 16
    • 08 00 → 0x0008 → PDSMemory = 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions