Repository navigation
Conversation
539fb7f to
d93519e
Compare
lgirdwood
left a comment
There was a problem hiding this comment.
Nice, clean source-tree check and contract write-up — fast (~1.5 s), config-independent, and the shrink-only [baseline] (already down to 105!) keeps platform headers from regressing.
A few edge cases found while reviewing scripts/check_platform_headers.py and cross-checking --contract against src/kernel/platform/README.md:
Tree.contract()drops macros wrapped by generic#defines (enc is not None), so--contractmissesPLATFORM_PAGE_TABLE_SIZE(src/kernel/ipc/ipc3/host-page-table.c),HEAP_{SYSTEM,RUNTIME,BUFFER}_SIZE(zephyr/lib/alloc.c), andMAILBOX_DEBUG_BASE(src/include/sof/debug/debug.h).- Host source & Zephyr include order (
HOST/FW_INCLUDE):src/kernel/drivers/host/(if(CONFIG_LIBRARY)) is missing fromHOST, andFW_INCLUDEputs'src/include'ahead of'zephyr/include', causingunreachable()to skipzephyr/include/wrapper headers when a same-named header exists insrc/include/. - Include guards separated by blank/comment lines:
lines[i - 2]misses#ifndef GUARDwhen a blank line or/* ... */comment sits before#define GUARD, whileIFNDEFstill ignores the#ifndeftoken, which falsely flags valid include guards as unused. - Local working-tree ergonomics:
git_files()raisesFileNotFoundErroron unstagedrmdeletions when testing cleanups locally (andscripts/check_platform_headers.pyis committed100644despite its#!/usr/bin/env python3shebang). src/kernel/platform/README.mdcontract table:PLATFORM_PAGE_TABLE_SIZE(platform/platform.h) andSRAM_OUTBOX_SIZE/SRAM_STREAM_BASE/MAILBOX_DEBUG_BASE(platform/lib/mailbox*.h) are used by generic code when IPC3 /CONFIG_LIBRARY_MANAGER/CONFIG_USERSPACE/CONFIG_DEBUGare enabled.
| HOST = ('posix/', 'tools/', 'test/cmocka/', 'src/kernel/arch/host/') | ||
| CODE_EXTS = ('.c', '.h', '.cpp', '.cc', '.hpp', '.S', '.ld', '.ld.in', '.x.in') | ||
| SOURCE_EXTS = ('.c', '.cpp', '.cc', '.S') | ||
| # include directories that every build has, besides the platform ones | ||
| FW_INCLUDE = ['src/include', 'src/kernel/include', 'zephyr/include', 'third_party/include', | ||
| 'src/kernel/arch/xtensa/include', 'tools/rimage/src/include'] |
There was a problem hiding this comment.
Two small build-layout fixes here:
src/kernel/drivers/CMakeLists.txtcompilessrc/kernel/drivers/host/only underif(CONFIG_LIBRARY)(just likesrc/kernel/arch/host/). Without'src/kernel/drivers/host/'inHOST,generic()treats those files as generic firmware code visible to every DSP platform.- Zephyr firmware builds put
zephyr/includebeforesrc/includeso headers likezephyr/include/sof/trace/trace.hoverridesrc/include/sof/trace/trace.h(matching howHOST_INCLUDEputs'posix/include'first). Becauseunreachable()breaks on the first candidate inself.fileset, listing'src/include'before'zephyr/include'skipszephyr/include/wrapper headers.
| HOST = ('posix/', 'tools/', 'test/cmocka/', 'src/kernel/arch/host/') | |
| CODE_EXTS = ('.c', '.h', '.cpp', '.cc', '.hpp', '.S', '.ld', '.ld.in', '.x.in') | |
| SOURCE_EXTS = ('.c', '.cpp', '.cc', '.S') | |
| # include directories that every build has, besides the platform ones | |
| FW_INCLUDE = ['src/include', 'src/kernel/include', 'zephyr/include', 'third_party/include', | |
| 'src/kernel/arch/xtensa/include', 'tools/rimage/src/include'] | |
| HOST = ('posix/', 'tools/', 'test/cmocka/', 'src/kernel/arch/host/', 'src/kernel/drivers/host/') | |
| CODE_EXTS = ('.c', '.h', '.cpp', '.cc', '.hpp', '.S', '.ld', '.ld.in', '.x.in') | |
| SOURCE_EXTS = ('.c', '.cpp', '.cc', '.S') | |
| # include directories that every build has, besides the platform ones | |
| FW_INCLUDE = ['zephyr/include', 'src/include', 'src/kernel/include', 'third_party/include', | |
| 'src/kernel/arch/xtensa/include', 'tools/rimage/src/include'] |
There was a problem hiding this comment.
Done in b96bda3, as suggested: src/kernel/drivers/host/ is now host-only, and FW_INCLUDE searches zephyr/include first. I checked the order on a real PTL compile line (zephyr/include is the 10th -I, src/include the 23rd), which matches the zephyr/include/sof/trace/trace.h wrapper including the src/include one. The findings on main are unchanged (105).
| def git_files(): | ||
| out = subprocess.run(['git', 'ls-files'], cwd=SOF_ROOT, check=True, | ||
| capture_output=True, text=True).stdout | ||
| return [f for f in out.split() if f.endswith(CODE_EXTS)] |
There was a problem hiding this comment.
If a developer deletes a tracked header/source locally with rm (unstaged) before git rm while testing a cleanup, git ls-files still lists it and Source.__init__ crashes with FileNotFoundError (compare scripts/check_kernel_includes.py, which checks os.path.isfile(...) and uses out.splitlines()).
(Also minor: scripts/check_platform_headers.py has mode 100644 despite its #!/usr/bin/env python3 shebang, unlike 100755 on scripts/check_kernel_includes.py; and posixpath instead of os.path in unreachable() avoids \ vs / mismatches if run on Windows.)
| def git_files(): | |
| out = subprocess.run(['git', 'ls-files'], cwd=SOF_ROOT, check=True, | |
| capture_output=True, text=True).stdout | |
| return [f for f in out.split() if f.endswith(CODE_EXTS)] | |
| def git_files(): | |
| out = subprocess.run(['git', 'ls-files'], cwd=SOF_ROOT, check=True, | |
| capture_output=True, text=True).stdout | |
| return [f for f in out.splitlines() | |
| if f.endswith(CODE_EXTS) and os.path.isfile(os.path.join(SOF_ROOT, f))] |
There was a problem hiding this comment.
Done in b96bda3: git_files() uses splitlines() and skips files missing from the work tree, so deleting a tracked header with rm before git rm no longer crashes (tested). unreachable() builds paths with posixpath, and the script is now mode 100755.
| if i >= 2 and re.match(r'\s*#\s*ifndef\s+' + cur + r'\b', lines[i - 2]): | ||
| self.guards.add(cur) |
There was a problem hiding this comment.
If a blank line or /* ... */ comment (which line 108 turns into blank lines) sits between #ifndef GUARD and #define GUARD, lines[i - 2] is empty so GUARD is not added to self.guards — while IFNDEF on line 131 + lines 248–250 still ignore the #ifndef GUARD token, causing valid include guards to fail --strict as unused macros. Checking the previous non-blank line avoids the false positive while still catching mismatched #ifndef/#define guard typos:
| if i >= 2 and re.match(r'\s*#\s*ifndef\s+' + cur + r'\b', lines[i - 2]): | |
| self.guards.add(cur) | |
| prev = next((l for l in reversed(lines[:i - 1]) if l.strip()), '') | |
| if re.match(r'\s*#\s*ifndef\s+' + cur + r'\b', prev): | |
| self.guards.add(cur) |
There was a problem hiding this comment.
Done in b96bda3 with your suggestion: a guard is recognised when the previous non-blank line is its #ifndef. Tested with a temporary header: a guard with a blank line and a comment before its #define is no longer reported, and a misspelled guard (#ifndef __X_H__ / #define __X_H___, like the imx93 clk.h one fixed in #11537) still is. No current platform header has that layout, so the baseline is unchanged.
| fw, host = set(), set() | ||
| for f in self.files: | ||
| if self.generic(f) or f.startswith(HOST): | ||
| used = {t for t, enc in self.src(f).uses if enc is None} | ||
| (host if f.startswith(HOST) else fw).update(used) |
There was a problem hiding this comment.
Filtering if enc is None here drops platform macros that generic code uses inside a live #define wrapper (unlike macros(), which computes transitive liveness across enc). As a result, --contract currently misses:
PLATFORM_PAGE_TABLE_SIZE(platform/platform.h, used insrc/kernel/ipc/ipc3/host-page-table.cinsideHOST_PAGE_TABLE_MAX_PAGES),HEAP_BUFFER_SIZE,HEAP_RUNTIME_SIZE,HEAP_SYSTEM_SIZE(platform/lib/memory.h, used inzephyr/lib/alloc.cinsideHEAPMEM_SIZE), andMAILBOX_DEBUG_BASE(platform/lib/mailbox.h, used viamailbox_get_debug_base()insrc/include/sof/debug/debug.h).
Propagating liveness through enclosing macros keeps --contract accurate without pulling in dead generic wrappers like mailbox_get_exception_base():
| fw, host = set(), set() | |
| for f in self.files: | |
| if self.generic(f) or f.startswith(HOST): | |
| used = {t for t, enc in self.src(f).uses if enc is None} | |
| (host if f.startswith(HOST) else fw).update(used) | |
| def alive_in(files): | |
| uses = defaultdict(list) | |
| for f in files: | |
| s = self.src(f) | |
| for t, enc in s.uses: | |
| if enc == '#ifndef': | |
| if t in s.defines: | |
| continue | |
| enc = None | |
| uses[t].append(enc) | |
| alive = {t for t, es in uses.items() if None in es} | |
| changed = True | |
| while changed: | |
| changed = False | |
| for t, es in uses.items(): | |
| if t not in alive and any(e in alive for e in es): | |
| alive.add(t) | |
| changed = True | |
| return alive | |
| fw = alive_in([f for f in self.files if self.generic(f)]) | |
| host = alive_in([f for f in self.files | |
| if (self.generic(f) and not f.startswith(('zephyr/', 'app/'))) or f.startswith(HOST)]) |
There was a problem hiding this comment.
Done in b96bda3 with your alive_in(): uses are followed through the generic macros that wrap them, for firmware and for the host builds. --contract now lists PLATFORM_PAGE_TABLE_SIZE, HEAP_{SYSTEM,RUNTIME,BUFFER}_SIZE and MAILBOX_DEBUG_BASE, and still leaves out dead wrappers such as mailbox_get_exception_base().
|
|
||
| | Include name | Included by | What generic code uses | | ||
| |---|---|---| | ||
| | `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` | |
There was a problem hiding this comment.
src/kernel/ipc/ipc3/host-page-table.c also uses PLATFORM_PAGE_TABLE_SIZE from platform/platform.h (falling back to HOST_PAGE_SIZE when unset on host builds); --contract missed it due to the enc is None filter in Tree.contract():
| | `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` | | |
| | `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `PLATFORM_PAGE_TABLE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` | |
There was a problem hiding this comment.
Done in c5b511c: PLATFORM_PAGE_TABLE_SIZE added to the platform/platform.h row. The fixed --contract now prints it too.
| | `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` | | ||
| | `platform/lib/memory.h` | `sof/lib/memory.h` | `PLATFORM_DCACHE_ALIGN`, `HOST_PAGE_SIZE`, `SOF_STACK_SIZE`, `host_to_local()`, `local_to_host()`; where the platform has them: the heap sizes (`HEAPMEM_SIZE`, `HEAP_*_SIZE`), `SRAM_BANK_SIZE`, `LP_SRAM_SIZE`, `IMR_BOOT_LDR_MANIFEST_BASE`, `DMA0_BASE`/`DMA0_SIZE`, `MU_BASE` | | ||
| | `platform/lib/clk.h` | `rtos/clk.h` | `NUM_CLOCKS`, `NUM_CPU_FREQ`, `CPU_DEFAULT_IDX`, `CPU_LOWEST_FREQ_IDX`, `CLK_MAX_CPU_HZ`, `CLK_SSP`, `PRIMARY_CORE_BASE_CPS_USAGE`, `SECONDARY_CORE_BASE_CPS_USAGE` | | ||
| | `platform/lib/mailbox.h`, `platform/lib/mailbox_layout.h` (in `src/platform`) | `sof/lib/mailbox.h` | the host windows `MAILBOX_{DSPBOX,HOSTBOX,STREAM}_{BASE,SIZE}`, `MAILBOX_SW_REG_{BASE,SIZE}`; `mailbox_sw_reg_read()`, `mailbox_sw_reg_read64()`, `mailbox_sw_reg_write()`, `mailbox_sw_regs_write()` | |
There was a problem hiding this comment.
Generic code also uses SRAM_OUTBOX_SIZE (src/kernel/library_manager/lib_notification.c with CONFIG_LIBRARY_MANAGER), SRAM_STREAM_BASE (zephyr/lib/userspace_helper.c for IPC3 userspace), and MAILBOX_DEBUG_BASE (src/include/sof/debug/debug.h under CONFIG_DEBUG) from the mailbox/layout headers:
| | `platform/lib/mailbox.h`, `platform/lib/mailbox_layout.h` (in `src/platform`) | `sof/lib/mailbox.h` | the host windows `MAILBOX_{DSPBOX,HOSTBOX,STREAM}_{BASE,SIZE}`, `MAILBOX_SW_REG_{BASE,SIZE}`; `mailbox_sw_reg_read()`, `mailbox_sw_reg_read64()`, `mailbox_sw_reg_write()`, `mailbox_sw_regs_write()` | | |
| | `platform/lib/mailbox.h`, `platform/lib/mailbox_layout.h` (in `src/platform`) | `sof/lib/mailbox.h` | the host windows `MAILBOX_{DSPBOX,HOSTBOX,STREAM,DEBUG}_{BASE,SIZE}`, `MAILBOX_SW_REG_{BASE,SIZE}`, and where used `SRAM_OUTBOX_SIZE`, `SRAM_STREAM_BASE`; `mailbox_sw_reg_read()`, `mailbox_sw_reg_read64()`, `mailbox_sw_reg_write()`, `mailbox_sw_regs_write()` | |
There was a problem hiding this comment.
Done in c5b511c, with one change from the suggestion: generic code uses MAILBOX_DEBUG_BASE but not MAILBOX_DEBUG_SIZE (--contract lists only the base), so the row names MAILBOX_DEBUG_BASE on its own instead of adding the whole DEBUG window. SRAM_OUTBOX_SIZE and SRAM_STREAM_BASE are added "where used", as suggested.
The platform headers collected headers and macros that no code uses any more, and new platforms copy them from the existing ones. Add check_platform_headers.py to find them in the source tree, without building: - a platform header is unused when nothing that can be built for its platform can include it; - a platform macro is unused when its name appears nowhere in the code that can be built together with that platform's headers: the generic SOF code, the Zephyr glue, and the platform's own platform and vendor driver directories (the host-only code for the host library platform). Text matching counts every #if branch, so the result does not depend on the configuration. Include guards, token-pasted names, the _BASE/_SIZE and _START/_END partners of used macros, mailbox windows with a used member and the vendor-generated Xtensa core headers are not reported. scripts/platform_headers.txt lists the platforms that share header directories, their vendor driver directories, and a baseline of the current 105 findings. With --strict the script fails on findings that are not in the baseline, so the baseline can only shrink. --contract prints the macros that generic code uses from each platform header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Describe the platform headers that generic code includes, and what it uses from each of them, so that a new platform only provides what is needed: no trace or PM runtime header, no DMA trace settings and no XTOS memory layout. Also describe scripts/check_platform_headers.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Run scripts/check_platform_headers.py --strict in the codestyle workflow, next to the kernel include check. It fails when a change adds a platform header or macro that nothing uses, and passes for the known ones listed in its baseline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
d93519e to
b30cf95
Compare
|
Thanks for the review. I addressed all six points and force-pushed b30cf95 (rebased on d80a052):
The findings on main are unchanged (105, all in the baseline). |
PR 11538: test resultsRun date: 2026-11-26 20:26 UTC Tested commit: b30cf95ac4a02ba9671c64441172e4c2109049da |
Add a CI check that stops new unused platform headers and macros, and document what a platform header has to provide. This is the last part of the platform header cleanup in #11517 step 1, after #11528, #11529, #11532, #11534 and #11537. Without a check, new platforms copy the old templates (trace headers, PM runtime stubs, DMA trace settings, XTOS memory layouts) and the unused code comes back.
Patches
scripts: add a check for unused platform headers and macros.check_platform_headers.pyworks on the source tree and needs no build; it runs in about 1.5 s.#includefrom the platform's sources with the platform's include path.#ifbranch, so the result is the same for every Kconfig._BASE/_SIZEand_START/_ENDpartners of used macros, mailbox windows with a used member, and the vendor-generated Xtensa core headers ininclude/arch/.scripts/platform_headers.txtlists the platforms that share header directories (intel/ace,intel/cavs,amd/common), their vendor driver directories, and a[baseline]of today's 105 findings.--strictfails on findings that are not in the baseline, so the baseline can only shrink. Entries that are no longer found are reported,--update-baselinerewrites the list, and--contractprints what generic code uses from each platform header.platform: document the platform header contract. A table insrc/kernel/platform/README.mdlists each include name that generic code uses, who includes it, and which macros and functions come from it. It also says what a new platform does not need..github: check for new unused platform headers and macros. Runs the check with--strictin the codestyle workflow, next to the kernel include check.Baseline
The 105 known findings are:
CPU_LPRO_FREQ_IDX, the sideband IPC fields and the qemu_xtensa trace window.When a later change removes one of them, the check reports the entry as no longer found but does not fail. The entry can then be dropped with
--update-baseline.Functional change
None. Only a script, a manifest, documentation and a CI job.
Testing
--strict. It finds no unused headers, because platform: remove unused platform headers #11528 removed them.--strictand reported their removed entries (97 and 138) as no longer found; neither added a finding. Rebased on main with both merged, the baseline is down to 105.DAI_BASE,DMA_ID_HOSTandPLATFORM_IPC_INTERRUPT(only the i.MX IPC driver uses it) confirmed them.meteorlake/platform.hand a new header that nothing includes to imx95 makes--strictfail with both listed.check_kernel_includes.py --sources --strictpasses, and the documentation builds with no warnings.🤖 Generated with Claude Code