-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Optimize counter bitwidth in Foreach control #293
Open
kumasento
wants to merge
3
commits into
stanford-ppl:master
Choose a base branch
from
kumasento:dev-opt-cnt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import spatial.dsl._ | ||
|
||
@spatial object TestForeachCounter extends SpatialApp { | ||
|
||
def main(args: Array[String]): Unit = { | ||
// Loop upper bound | ||
val N = 128 | ||
|
||
// The DRAM | ||
val d = DRAM[Int](N) | ||
|
||
// DRAM content | ||
val data = Array.fill[Int](N)(0) | ||
setMem(d, data) | ||
|
||
Accel { | ||
val s = SRAM[Int](N) | ||
|
||
s load d(0::N) | ||
|
||
Foreach(N by 1) { i => s(i) = s(i) + i } | ||
|
||
d(0::N) store s | ||
} | ||
|
||
printArray(getMem(d), "Result: ") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package spatial.transform | ||
|
||
import argon._ | ||
import argon.node._ | ||
import argon.transform.MutateTransformer | ||
|
||
import spatial.lang._ | ||
import spatial.node._ | ||
import spatial.util.shouldMotionFromConditional | ||
import spatial.traversal.AccelTraversal | ||
import spatial.metadata.control._ | ||
import spatial.metadata.memory._ | ||
import spatial.metadata.blackbox._ | ||
|
||
import utils.math.log2Up | ||
|
||
import emul.FixedPoint | ||
|
||
case class CounterBitwidthTransformer(IR: State) extends MutateTransformer | ||
with AccelTraversal { | ||
|
||
/** Calculate the least bitwidth required for an integer. */ | ||
private def getBitwidth(x: Int): Int = log2Up(x.abs) | ||
|
||
/** Extract the content from Const and cast it to Int. */ | ||
private def constToInt(x: Sym[_]): Int = | ||
if (x.isConst) | ||
x.c.get.asInstanceOf[FixedPoint].toInt | ||
else | ||
throw new Exception(s"$x is not a Const.") | ||
|
||
/** Create a new CounterNew object with compact bitwidth. */ | ||
private def getOptimizedCounterNew(ctr: CounterNew[_]): CounterNew[_] = ctr match { | ||
case CounterNew(start, stop, step, par) => | ||
// we take the largest magnitude of start and stop to decide the boundary of bitwidth | ||
val begin = constToInt(start) | ||
val end = constToInt(stop) | ||
val bitwidth = math.max(getBitwidth(begin), getBitwidth(end)) | ||
|
||
// TODO: Find a better way that can map bitwidth to the exact Fix type | ||
if (bitwidth <= 7) { | ||
type T = Fix[TRUE,_8,_0] | ||
CounterNew[T](begin.to[T], end.to[T], constToInt(step).to[T], par) | ||
} else if (bitwidth <= 15) { | ||
type T = Fix[TRUE,_16,_0] | ||
CounterNew[T](begin.to[T], end.to[T], constToInt(step).to[T], par) | ||
} else { | ||
type T = Fix[TRUE,_32,_0] | ||
CounterNew[T](begin.to[T], end.to[T], constToInt(step).to[T], par) | ||
} | ||
case _ => ctr | ||
} | ||
|
||
/** Optimize a list of Counter. */ | ||
private def getOptimizeCounters(ctrs: Seq[Counter[_]]): Seq[Counter[_]] = { | ||
ctrs.map { | ||
case Op(ctr: CounterNew[_]) => stage(getOptimizeCounterNew(ctr)) | ||
} | ||
} | ||
|
||
override def transform[A:Type](lhs: Sym[A], rhs: Op[A])(implicit ctx: SrcCtx): Sym[A] = rhs match { | ||
case AccelScope(_) => | ||
inAccel { super.transform(lhs, rhs) } | ||
|
||
case OpForeach(ens, cchain, blk, iters, stopWhen) if inHw => | ||
val newctrs = getOptimizedCounters(cchain.counters) | ||
val newcchain = stageWithFlow(CounterChainNew(newctrs)){ lhs2 => transferData(lhs, lhs2)} | ||
|
||
stageWithFlow( | ||
OpForeach( | ||
ens, | ||
newcchain, | ||
stageBlock{blk.stms.foreach(visit)}, | ||
iters, | ||
stopWhen) | ||
){lhs2 => transferData(lhs, lhs2)} | ||
|
||
case _ => | ||
dbgs(s"visiting $lhs = $rhs"); | ||
super.transform(lhs, rhs) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattfel1 Hi there, I feel that it would be tedious to implement all the bitwidth-to-type cast and there should be a better way that I'm not aware of. Maybe you've met this scenario before and have a good way to deal with it? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I don't think there is a non-tedious way to do this.
Since each bitwidth is its own trait (argon/lang/types/CustomBitWidths.scala), its painful to work with. Some people have used quasiquotes for this problem before but there isn't a nice way that I know of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mattfel1 ! In my latest update I manually added all the mappings for different bit-width values. Hope it looks fine.