[llvm-branch-commits] [llvm] [llvm-profgen] Add ITM data trace support (PR #230310)

Kunal Pathak via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 4 12:01:36 PST 2026


================
@@ -88,32 +128,38 @@ class ETMDecoderImpl : public ETMDecoder {
         Decoder->CurrentCallback->processInstructionRange(Start, End - 1);
       }
     }
+
+    // Process data addresses from the ITM trace.
+    if (Element->elem_type == OCSD_GEN_TRC_ELEM_ITMTRACE &&
+        Element->swt_itm.pkt_type == DWT_PAYLOAD) {
----------------
kunalspathak wrote:


I think this needs to look at the discriminator too. `DWT_PAYLOAD` is every DWT hardware-source packet -- OpenCSD's ITM decoder [emits all `ITM_PKT_DWT` packets with that type](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/source/itm/trc_pkt_decode_itm.cpp#L182-L189) and just puts the discriminator in [`swt_itm.payload_src_id`](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/include/opencsd/trc_gen_elem_types.h#L115-L131). Going by OpenCSD's own packet printer ([`ItmTrcPacket::printDWTPacket`](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/source/itm/trc_pkt_elem_itm.cpp#L186-L256)):

- 0: [event counter](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/source/itm/trc_pkt_elem_itm.cpp#L198)
- 1: [exception trace](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/source/itm/trc_pkt_elem_itm.cpp#L226-L230)
- 2: [PC sample](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/source/itm/trc_pkt_elem_itm.cpp#L231-L235)
- 8/10/12/14: data trace PC value
- 9/11/13/15: data trace address  <- the only ones we want
- 16-23: data trace data value

(the [data trace split](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/source/itm/trc_pkt_elem_itm.cpp#L236-L251) is type = `(id >> 3) & 3`, rw = `id & 1`; type 1 + rw 1 is the address packet)

So right now event counters, exception numbers and data *values* all go through `resolveDataAddress()`, and the 1/2-byte ones get upper bits filled from the data sections, which makes them very likely to land on some global.

Repro with the binary from Inputs/etm-opencsd.yaml (global_var @ 0x20001000, global_arr @ 0x20001004 size 16), one packet per trace after the ITM sync `00 00 00 00 00 80`. Header byte is `(id << 3) | 0x4 | size` with size 1/2/3 = 1/2/4 bytes ([packet processor](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/source/itm/trc_pkt_proc_itm.cpp#L241-L245), [discriminator](https://github.com/Linaro/OpenCSD/blob/59f15320637ce6e510604e7bed550d7ad109d15d/decoder/source/itm/trc_pkt_proc_itm.cpp#L316)):

| packet (header + payload)                       | expected       | got                            |
| ----------------------------------------------- | -------------- | ------------------------------ |
| `4F 00 10 00 20` (id 9, address 0x20001000)     | global_var:1   | global_var:1                   |
| `05 04` (id 0, event counter)                   | (empty)        | global_arr:1                   |
| `0E 0F 10` (id 1, SysTick exception entry)      | (empty)        | global_arr:1                   |
| `85 05` (id 16, data value read = 5)            | (empty)        | global_arr:1                   |
| `8F 08 10 00 20` (id 17, value write = pointer) | (empty)        | global_arr:1                   |
| `4F 00 10 00 20` + `85 07` (address + value)    | global_var:1   | global_var:1, global_arr:1     |

The last one is the case I'd expect people to actually hit: if the comparator is set up to emit address + data value, every access gets counted once correctly and once more against whatever global the value happens to alias. PC samples and PC values only come out right because the addresses are in code and get filtered by `addressIsCode()` later.

Something like this fixes all of the above for me, and itm-data.test still passes (it uses id 9):

```cpp
  // DWT hardware source packets share one generic element type; the
  // discriminator ID tells them apart. Data trace packets use IDs 8-23, where
  // bits [4:3] == 0b01 select PC value / data address packets and bit 0 selects
  // the data address variant.
  static bool isDataAddressPacket(const swt_itm_info &ITM) {
    uint8_t ID = ITM.payload_src_id;
    return ((ID >> 3) & 0x3) == 0x1 && (ID & 0x1);
  }
  ...
    if (Element->elem_type == OCSD_GEN_TRC_ELEM_ITMTRACE &&
        Element->swt_itm.pkt_type == DWT_PAYLOAD &&
        isDataAddressPacket(Element->swt_itm)) {
```

Would be good to extend itm-data.test with a few non-address DWT packets mixed in (event counter / exception / data value) so this stays covered.

(Assisted by AI for small repro: GPT-6 Astra)

https://github.com/llvm/llvm-project/pull/230310


More information about the llvm-branch-commits mailing list