Don't issue gather/scatter warnigns in the 'extra' bits of foreach loops.

With AOS data, we can often coalesce the accesses into gathers for the main
part of foreach loops but only fail on the last bits where the mask is not
all on (since the coalescing code doesn't handle mixed masks, yet.) Before,
we'd report success with coalescing and then also report that gathers were needed
for the same accesses that were coalesced, which was a) confusing, and b)
didn't accurately represent what was going on for the majority of the loop
iterations.
This commit is contained in:
Matt Pharr
2012-03-19 15:08:35 -07:00
parent d74cc6397b
commit 7e954e4248
4 changed files with 45 additions and 17 deletions

18
ctx.cpp
View File

@@ -245,6 +245,8 @@ FunctionEmitContext::FunctionEmitContext(Function *func, Symbol *funSym,
StoreInst(llvm::Constant::getNullValue(LLVMTypes::VoidPointerType), StoreInst(llvm::Constant::getNullValue(LLVMTypes::VoidPointerType),
launchGroupHandlePtr); launchGroupHandlePtr);
disableGSWarningCount = 0;
const Type *returnType = function->GetReturnType(); const Type *returnType = function->GetReturnType();
if (!returnType || Type::Equal(returnType, AtomicType::Void)) if (!returnType || Type::Equal(returnType, AtomicType::Void))
returnValuePtr = NULL; returnValuePtr = NULL;
@@ -1106,6 +1108,19 @@ FunctionEmitContext::InForeachLoop() const {
} }
void
FunctionEmitContext::DisableGatherScatterWarnings() {
++disableGSWarningCount;
}
void
FunctionEmitContext::EnableGatherScatterWarnings() {
--disableGSWarningCount;
}
bool bool
FunctionEmitContext::initLabelBBlocks(ASTNode *node, void *data) { FunctionEmitContext::initLabelBBlocks(ASTNode *node, void *data) {
LabeledStmt *ls = dynamic_cast<LabeledStmt *>(node); LabeledStmt *ls = dynamic_cast<LabeledStmt *>(node);
@@ -2492,6 +2507,7 @@ FunctionEmitContext::gather(llvm::Value *ptr, const PointerType *ptrType,
// Add metadata about the source file location so that the // Add metadata about the source file location so that the
// optimization passes can print useful performance warnings if we // optimization passes can print useful performance warnings if we
// can't optimize out this gather // can't optimize out this gather
if (disableGSWarningCount == 0)
addGSMetadata(call, currentPos); addGSMetadata(call, currentPos);
return BitCastInst(call, llvmReturnType, "gather_bitcast"); return BitCastInst(call, llvmReturnType, "gather_bitcast");
@@ -2801,6 +2817,8 @@ FunctionEmitContext::scatter(llvm::Value *value, llvm::Value *ptr,
args.push_back(value); args.push_back(value);
args.push_back(mask); args.push_back(mask);
llvm::Value *inst = CallInst(scatterFunc, NULL, args); llvm::Value *inst = CallInst(scatterFunc, NULL, args);
if (disableGSWarningCount == 0)
addGSMetadata(inst, currentPos); addGSMetadata(inst, currentPos);
} }

11
ctx.h
View File

@@ -230,6 +230,13 @@ public:
bool InForeachLoop() const; bool InForeachLoop() const;
/** Temporarily disables emission of performance warnings from gathers
and scatters from subsequent code. */
void DisableGatherScatterWarnings();
/** Reenables emission of gather/scatter performance warnings. */
void EnableGatherScatterWarnings();
void SetContinueTarget(llvm::BasicBlock *bb) { continueTarget = bb; } void SetContinueTarget(llvm::BasicBlock *bb) { continueTarget = bb; }
/** Step through the code and find label statements; create a basic /** Step through the code and find label statements; create a basic
@@ -644,6 +651,10 @@ private:
tasks launched from the current function. */ tasks launched from the current function. */
llvm::Value *launchGroupHandlePtr; llvm::Value *launchGroupHandlePtr;
/** Nesting count of the number of times calling code has disabled (and
not yet reenabled) gather/scatter performance warnings. */
int disableGSWarningCount;
std::map<std::string, llvm::BasicBlock *> labelMap; std::map<std::string, llvm::BasicBlock *> labelMap;
static bool initLabelBBlocks(ASTNode *node, void *data); static bool initLabelBBlocks(ASTNode *node, void *data);

17
opt.cpp
View File

@@ -2382,8 +2382,7 @@ GSToLoadStorePass::runOnBasicBlock(llvm::BasicBlock &bb) {
continue; continue;
SourcePos pos; SourcePos pos;
bool ok = lGetSourcePosFromMetadata(callInst, &pos); lGetSourcePosFromMetadata(callInst, &pos);
Assert(ok);
llvm::Value *base = callInst->getArgOperand(0); llvm::Value *base = callInst->getArgOperand(0);
llvm::Value *varyingOffsets = callInst->getArgOperand(1); llvm::Value *varyingOffsets = callInst->getArgOperand(1);
@@ -2742,8 +2741,7 @@ static void
lCoalescePerfInfo(const std::vector<llvm::CallInst *> &coalesceGroup, lCoalescePerfInfo(const std::vector<llvm::CallInst *> &coalesceGroup,
const std::vector<CoalescedLoadOp> &loadOps) { const std::vector<CoalescedLoadOp> &loadOps) {
SourcePos pos; SourcePos pos;
bool ok = lGetSourcePosFromMetadata(coalesceGroup[0], &pos); lGetSourcePosFromMetadata(coalesceGroup[0], &pos);
Assert(ok);
// Create a string that indicates the line numbers of the subsequent // Create a string that indicates the line numbers of the subsequent
// gathers from the first one that were coalesced here. // gathers from the first one that were coalesced here.
@@ -2758,13 +2756,14 @@ lCoalescePerfInfo(const std::vector<llvm::CallInst *> &coalesceGroup,
for (int i = 1; i < (int)coalesceGroup.size(); ++i) { for (int i = 1; i < (int)coalesceGroup.size(); ++i) {
SourcePos p; SourcePos p;
bool ok = lGetSourcePosFromMetadata(coalesceGroup[i], &p); bool ok = lGetSourcePosFromMetadata(coalesceGroup[i], &p);
Assert(ok); if (ok) {
char buf[32]; char buf[32];
sprintf(buf, "%d", p.first_line); sprintf(buf, "%d", p.first_line);
strcat(otherPositions, buf); strcat(otherPositions, buf);
if (i < (int)coalesceGroup.size() - 1) if (i < (int)coalesceGroup.size() - 1)
strcat(otherPositions, ", "); strcat(otherPositions, ", ");
} }
}
strcat(otherPositions, ") "); strcat(otherPositions, ") ");
} }
@@ -3460,8 +3459,7 @@ GatherCoalescePass::runOnBasicBlock(llvm::BasicBlock &bb) {
continue; continue;
SourcePos pos; SourcePos pos;
bool ok = lGetSourcePosFromMetadata(callInst, &pos); lGetSourcePosFromMetadata(callInst, &pos);
Assert(ok);
Debug(pos, "Checking for coalescable gathers starting here..."); Debug(pos, "Checking for coalescable gathers starting here...");
llvm::Value *base = callInst->getArgOperand(0); llvm::Value *base = callInst->getArgOperand(0);
@@ -3680,11 +3678,10 @@ PseudoGSToGSPass::runOnBasicBlock(llvm::BasicBlock &bb) {
// Get the source position from the metadata attached to the call // Get the source position from the metadata attached to the call
// instruction so that we can issue PerformanceWarning()s below. // instruction so that we can issue PerformanceWarning()s below.
SourcePos pos; SourcePos pos;
bool ok = lGetSourcePosFromMetadata(callInst, &pos); bool gotPosition = lGetSourcePosFromMetadata(callInst, &pos);
Assert(ok);
callInst->setCalledFunction(info->actualFunc); callInst->setCalledFunction(info->actualFunc);
if (g->target.vectorWidth > 1) { if (gotPosition && g->target.vectorWidth > 1) {
if (info->isGather) if (info->isGather)
PerformanceWarning(pos, "Gather required to compute value in expression."); PerformanceWarning(pos, "Gather required to compute value in expression.");
else else

View File

@@ -1745,7 +1745,9 @@ ForeachStmt::EmitCode(FunctionEmitContext *ctx) const {
ctx->SetCurrentBasicBlock(bbMaskedBody); { ctx->SetCurrentBasicBlock(bbMaskedBody); {
ctx->AddInstrumentationPoint("foreach loop body (masked)"); ctx->AddInstrumentationPoint("foreach loop body (masked)");
ctx->SetContinueTarget(bbMaskedBodyContinue); ctx->SetContinueTarget(bbMaskedBodyContinue);
ctx->DisableGatherScatterWarnings();
stmts->EmitCode(ctx); stmts->EmitCode(ctx);
ctx->EnableGatherScatterWarnings();
ctx->BranchInst(bbMaskedBodyContinue); ctx->BranchInst(bbMaskedBodyContinue);
} }
ctx->SetCurrentBasicBlock(bbMaskedBodyContinue); { ctx->SetCurrentBasicBlock(bbMaskedBodyContinue); {