0

If each CPU cache line has 64 bytes, one data has 8 bytes. When the data's physical address is at block offset 60, i.e., it would be stored across 2 cache lines.

                           +---------+
                           | data    |
+-------------------------------+-------------------------------+
| cache line 1                  | cache line 2                  |
+-------------------------------+-------------------------------+
 64 bytes                         64 bytes

How will cache load & store the data? If there is a load instruction to fetch data: ld rd,offset(rs1), will these 2 cache lines get hit together?

Or the compiler is smart enough so the load instruction will be broken into 2 instructions to load 4 bytes data at each time?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Yangmin Zhao
  • 21
  • 1
  • 1
  • Most compilers for most languages will require that 8-byte integers are always aligned by 8. e.g. `alignof(int64_t) == 8`, especially on RV64. But that doesn't apply for `struct { char c[8]; };` or `struct { int a; char buf[4];};` or something; a compiler might still want to copy a whole struct with one load/store instead of copying each byte separately. So the choice in that case depends on whether the compiler expects the CPU to handle unaligned loads efficiently. RISC-V requires that they are handled correctly, although possibly by a fault handler in which case it'd be very slow. – Peter Cordes May 16 '23 at 09:25

1 Answers1

0

This is a subset of more general situation of unaligned access. Some CPU architectures or CPU implementations choose not to support unaligned accesses -- in this case, the CPU will catch an exception. If it has been chosen to support unaligned accesses, CPU usually will break the operation into several aligned accesses and then reconstruct unaligned data from the result of both accesses. For writes, that will also mean either writing data words into cache or external memory with masked bytes or resorting to read-modify-write approach.

lvd
  • 793
  • 3
  • 12
  • Thanks! For CPU supports unaligned data, I'm curious that who is responsible for breaking data accesses and doing the reconstruction? Is it the compiler or CPU? – Yangmin Zhao May 18 '23 at 07:09