performance - Cortex M4 LDR/STR 时序

标签 performance assembly arm cpu-architecture cortex-m

我正在阅读 Cortex M4 TRM 以了解指令执行周期。但是,那里有一些令人困惑的描述

  • Table of Processor Instuctions , STR需要 2 个周期 .
  • 稍后在 Load/store timings , 表示

  • STR Rx,[Ry,#imm] is always one cycle, This is because the address generation is performed in the initial cycle, and the data store is performed at the same time as the next instruction is executing.


    If the store is to the write buffer, and the write buffer is full or not enabled, the next instruction is delayed until the store can complete.


    If the store is not to the write buffer, for example to the Code segment, and that transaction stalls, the impact on timing is only felt if another load or store operation is executed before completion


  • 还在 Load/store timings , 表示 LDR可以通过以下方式进行流水线处理 LDRSTR ,但是 STR无法按照说明进行流水线处理。

  • Other instructions cannot be pipelined after STR with register offset. STR can only be pipelined when it follows an LDR, but nothing can be pipelined after the store. Even a stalled STR normally only takes two cycles, because of the write buffer


    更具体的让我困惑的是:
    一季度。 1 2 好像互相冲突,多少个周期做STR实际上拿,1还是2? (虽然我的实验显示为 1)
    Q2。 2 表示如果商店经过write buffer并且它不可用,它仍然会停止流水线,但是如果存储绕过它,流水线可能只在加载/存储指令跟随时停止。闻起来像write buffer只会让事情变得更糟。这是有悖常理的。
    Q3。 3 意味着 STR无法使用以下指令进行流水线化,但是 2 意味着 STR在适当的条件下,始终使用以下指令进行流水线化。如何理解相互矛盾的陈述? (这里表示 STR 由于 write buffer 需要 2 个周期而不是 1 个周期)
    第 4 季度。我没有找到有关如何 write buffer 的更多信息已实现。缓冲区有多大?如何STR确定是使用它还是绕过它?

    最佳答案

    STR 类型
    请注意,在“加载/存储计时页面”上,第一条语句引用了 STR,其字面上偏移到基地址寄存器 ( STR Rx,[Ry,#imm] )。再往下,它指的是一个 STR,其寄存器偏移量到基地址寄存器 (STR R1,[R3,R2])。这是 STR 指令的两种不同变体。
    文字偏移 STR ( STR Rx,[Ry,#imm] )
    嗯,我想知道文档在说“总是 1 个周期”时是否具有误导性,因为随后添加了一个警告,这意味着它可能需要多个周期“......下一条指令被延迟,直到存储完成”
    我将尽力解释文档:

    STR Rx,[Ry,#imm] is always one cycle. This is because the address generation is performed in the initial cycle, and the data store is performed at the same time as the next instruction is executing. If the store is to the write buffer, and the write buffer is full or not enabled, the next instruction is delayed until the store can complete. If the store is to the write buffer, for example to the Code segment, and that transaction stalls, the impact on timing is only felt if another load or store operation is executed before completion.


    如果写缓冲区可用,我会假设第一个 STR 需要 1 个周期。如果它不可用,则下一条指令将被暂停,直到缓冲区可用。但是,如果缓冲区未使用,它将延迟下一条指令,直到总线事务完成。
    对于非连续 STR(第一个 STR),写缓冲区将是空的,并且指令需要 1 个周期。如果有 2 个连续的 STR 指令,第二个 STR 将在第一个 STR 写入缓冲区时立即开始。但是,如果第一个 STR 的总线事务停止并保留在写缓冲区中,则第二个 STR 将无法写入缓冲区并将阻止进一步的指令。然后当第一个 STR 的总线事务完成时,缓冲区被清空,第二个 STR 写入缓冲区,解锁下一条指令。
    停滞的总线事务,其中事务缓冲在写缓冲区中,不会影响非 STR 指令,因为它们不需要访问写缓冲区来完成。因此,总线停止的 STR 指令不会延迟进一步的指令,除非它是另一个 STR。但是,如果未使用写缓冲区,则停止的总线事务将延迟所有指令。
    指令集摘要页面将固定的“2”作为 STR 的周期数似乎有点偏离,但显然它不像这样可预测。
    寄存器偏移 STR ( STR R1,[R3,R2] )
    对于您对以下明显相互矛盾的陈述感到困惑,我与您站在一起:

    Other instructions cannot be pipelined after STR with register offset. STR can only be pipelined when it follows an LDR, but nothing can be pipelined after the store. Even a stalled STR normally only takes two cycles, because of the write buffer.


    因为这与页面上的第一个条款相矛盾。但是,我相信这是因为它指的是 2 种不同的 STR 类型,文字偏移量(第一个)和寄存器偏移量。寄存器偏移 STR 是之后不能允许流水线指令的寄存器。不过语言可能会更清楚。停顿的 STR 是什么意思,它是否指的是默认情况下总是停顿的寄存器偏移量 STR?这个停顿与写缓冲区不可用导致的停顿不同吗?在这里很容易迷路。
    我认为基本上一个寄存器偏移量 STR 至少是 2 个周期。如果写缓冲区不可用,或者事务没有缓冲并且总线停顿,它将阻塞并占用更多周期。
    写缓冲区大小
    大小为单个条目,见https://developer.arm.com/documentation/100166/0001/Programmers-Model/Write-buffer?lang=en

    To prevent bus wait cycles from stalling the processor during data stores, buffered stores to the DCode and System buses go through a one-entry write buffer. If the write buffer is full, subsequent accesses to the bus stall until the write buffer has drained.


    The write buffer is only used if the bus waits the data phase of the buffered store, otherwise the transaction completes on the bus.


    写缓冲区的用处
    就我的理解而言:如果 CPU 可以立即写入总线,那么它就不需要缓冲区,因为总线将立即空闲以供下一条指令使用。在像 M4 这样的高性能部件上,一些内存总线无法跟上 CPU 时钟速率,这意味着执行事务可能需要多个周期。也可能有使用相同总线的 DMA 单元。为了防止在总线事务完成之前停止 CPU,缓冲区提供了一个立即存储以使用该硬件然后在总线空闲时写入总线。

    关于performance - Cortex M4 LDR/STR 时序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63261379/

    相关文章:

    c++ - 在 C++ 中搜索文件中的字符串以获得非常大的输入的有效方法

    css - LESS、媒体查询和性能

    assembly - 为什么在 64 位模式下使用 'DS:' 段覆盖是非法的?

    uwp - VS 2017 社区 : Change target for existing project to ARM

    ios - 用于图像处理的 arm asm/neon 优化

    C#:使用任何持久性模型进行非常快速的对象搜索和检索

    c# - 更多的 CPU/核心是否有助于缩短 VS.NET 的构建时间?

    linux - 以前错误的未对齐调用现在可以工作了吗?

    c - asm 中的不可能约束

    ios - 我的build设置中缺少 arm6