Skip to content

Commit b205ac2

Browse files
committed
tensor,codegen: fix a bug where kernel cache could be modified
This commit fixes a bug where upon recompilation of an index statement, entries in the kernel cache could be inadvertently modified, leading to confusing segfaults. An example of the bug is included in the added test, where the second call to `c(i, j) = a(i, j)` would hit the cache, but then find a module that had code that corresponded to `c(i, j) = a(i, j) + b(i, j)`.
1 parent 468ad7f commit b205ac2

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

include/taco/codegen/module.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class Module {
2121
setJITTmpdir();
2222
}
2323

24-
void reset();
25-
2624
/// Compile the source into a library, returning its full path
2725
std::string compile();
2826

src/codegen/module.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,6 @@ void Module::setJITLibname() {
3232
libname[i] = chars[rand() % chars.length()];
3333
}
3434

35-
void Module::reset() {
36-
funcs.clear();
37-
moduleFromUserSource = false;
38-
header.str("");
39-
header.clear();
40-
source.str("");
41-
source.clear();
42-
}
43-
4435
void Module::addFunction(Stmt func) {
4536
funcs.push_back(func);
4637
}

src/tensor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,10 @@ void TensorBase::compile(taco::IndexStmt stmt, bool assembleWhileCompute) {
595595

596596
content->assembleFunc = lower(stmtToCompile, "assemble", true, false);
597597
content->computeFunc = lower(stmtToCompile, "compute", assembleWhileCompute, true);
598-
content->module->reset();
598+
// If we have to recompile the kernel, we need to create a new Module. Since
599+
// the module we are holding on to could have been retrieved from the cache,
600+
// we can't modify it.
601+
content->module = make_shared<Module>();
599602
content->module->addFunction(content->assembleFunc);
600603
content->module->addFunction(content->computeFunc);
601604
content->module->compile();

test/tests-tensor.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,18 @@ TEST(tensor, recompile) {
479479
ASSERT_TRUE(c.needsCompile());
480480
ASSERT_EQ(c.begin()->second, 42.0);
481481
}
482+
483+
TEST(tensor, cache) {
484+
auto dim = 2;
485+
IndexVar i("i"), j("j");
486+
Tensor<int> a("a", {dim, dim}, {Dense, Dense});
487+
Tensor<int> b("b", {dim, dim}, {Dense, Dense});
488+
Tensor<int> c("c", {dim, dim}, {Dense, Dense});
489+
// Add a computation to the cache.
490+
c(i, j) = a(i, j); c.evaluate();
491+
// Add a new computation to the cache.
492+
c(i, j) = a(i, j) + b(i, j); c.evaluate();
493+
// The addition of the new computation shouldn't have affected the cache's
494+
// ability to answer a request for the first query.
495+
c(i, j) = a(i, j); c.evaluate();
496+
}

0 commit comments

Comments
 (0)