看完了前面的系列,对于Stageable、StageableKey是如何起作用的应该有一定的了解。今天再来看下Stage中关于terminal的作用
》terminal
在Stage中,有关terminal的定义牵涉到两个terminal函数和一个LinkedHashSet:
def terminal(key : StageableKey) : StageableKey = { internals.stageableTerminal += key key } def terminal[T <: Data](key : Stageable[T], key2 : Any) : StageableKey = { terminal(StageableKey(key.asInstanceOf[Stageable[Data]], key2)) } val stageableTerminal = mutable.LinkedHashSet[StageableKey]()
可以看到,对一个stageable、stageableKey调用terminal,其会将数据压到stageableTerminal中。
我们之前说过,pipeline的构建核心在Pipeline中的build函数上。那么来看下在pipeline的build函数中stageableTerminal都起了什么作用。
stageableTerminal在build函数中出现了两次。第一次是pipeline的payload填充:
//Fill payload holes in the pipeline def propagateData(key : StageableKey, stage : Stage): Boolean ={ if(stage.internals.stageableTerminal.contains(key)) return false stage.stageableToData.get(key) match { case None => { val hits = ArrayBuffer[Stage]() for(m <- stageMasters(stage)){ if(propagateData(key, m)){ stage.apply(key) //Force creation hits += m } } hits.size match { case 0 => false case 1 => true case 2 => PendingError(s"$key at $stage has multiple drivers : ${hits.mkString(",")}"); false } } case Some(x) => true } }
我们前面提过,propagatedData用于向stage填充其前级有,后级stage中也有,但本级没有的stageable/stageableKey至stageableToData,可以看到,这里的处理一旦发现stageableKey在当前stage的stageableTerminal中包含,那么其将会不再向前级搜索,也就意味着这个信号的传递在当前Stage中止。
另一处出现的地方则是Interconnect stages:
for(c <- connections){ val stageables = (c.m.stageableToData.keys).filter(key => c.s.stageableToData.contains(key) && !c.m.stageableTerminal.contains(key)) var m = ConnectionPoint(c.m.output.valid, c.m.output.ready, stageables.map(c.m.outputOf(_)).toList) for((l, id) <- c.logics.zipWithIndex){ val s = if(l == c.logics.last) ConnectionPoint(c.s.input.valid, c.s.input.ready, stageables.map(c.s.stageableToData(_)).toList) else { ConnectionPoint(Bool(), (m.ready != null) generate Bool(), stageables.map(_.stageable.craft()).toList) } val area = l.on(m, s, clFlush(l), clFlushNext(l), clFlushNextHit(l), clThrowOne(l), clThrowOneHit(l)) if(c.logics.size != 1) area.setCompositeName(c, s"level_$id", true) else area.setCompositeName(c, true) m = s } }
可以看到,这里在选择master stage要传递到slave stage中的stageables时,是将stageableTerminal中的信号给过滤掉了,也就意味着在stageableTerminal对应的信号将不会进行Connection连接。也就意味着在进行Stage之间的连接时,会将相邻两级的stageableToData中共有的信号进行连接,如果该信号也同时被注册到了stageableTerminal中,那么该信号将会排除在外,即terminal提供了一个能够终止stageable在Stage之间传播连接的途径。
》Demo
来看下下面的例子:
case class Test2() extends Component{ val io=new Bundle{ val data_in=slave(Flow(Vec(UInt(8 bits),4))) val data_out=master(Flow(UInt(8 bits))) } noIoPrefix() val A,B,C=Stageable(UInt(8 bits)) val pip=new Pipeline{ val staeg0=new Stage{ this.internals.input.valid:=io.data_in.valid A:=io.data_in.payload(0)+io.data_in.payload(1) B:=io.data_in.payload(2)+io.data_in.payload(3) C:=io.data_in.payload(1)+io.data_in.payload(3) this.terminal(C,null) } val stage1=new Stage(Connection.M2S()){ C:=A+B io.data_out.payload:=C io.data_out.valid:=this.internals.output.valid } } }
我们在stage0中为C注册了termianl,此时对于stage0中的stageableToData包含三个元素A,B,C,而stageableTerminal中则包含了C。而对于Stage1,其stageableToData包含了A,B,C三个元素,那么也就意味着stage0中的C将不会传递到stage1中,仅有A、B两个元素会在stage之间连接。所有stage1中的C将会由stage1中的A+B驱动,而不是stage0中的C驱动。
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !