From 7e954e4248935094984abccd76d19989c08ea3ed Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Mon, 19 Mar 2012 15:08:35 -0700 Subject: [PATCH] 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. --- ctx.cpp | 22 ++++++++++++++++++++-- ctx.h | 11 +++++++++++ opt.cpp | 27 ++++++++++++--------------- stmt.cpp | 2 ++ 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/ctx.cpp b/ctx.cpp index e82c2877..5f5258e8 100644 --- a/ctx.cpp +++ b/ctx.cpp @@ -245,6 +245,8 @@ FunctionEmitContext::FunctionEmitContext(Function *func, Symbol *funSym, StoreInst(llvm::Constant::getNullValue(LLVMTypes::VoidPointerType), launchGroupHandlePtr); + disableGSWarningCount = 0; + const Type *returnType = function->GetReturnType(); if (!returnType || Type::Equal(returnType, AtomicType::Void)) returnValuePtr = NULL; @@ -1106,6 +1108,19 @@ FunctionEmitContext::InForeachLoop() const { } +void +FunctionEmitContext::DisableGatherScatterWarnings() { + ++disableGSWarningCount; +} + + +void +FunctionEmitContext::EnableGatherScatterWarnings() { + --disableGSWarningCount; +} + + + bool FunctionEmitContext::initLabelBBlocks(ASTNode *node, void *data) { LabeledStmt *ls = dynamic_cast(node); @@ -2492,7 +2507,8 @@ FunctionEmitContext::gather(llvm::Value *ptr, const PointerType *ptrType, // Add metadata about the source file location so that the // optimization passes can print useful performance warnings if we // can't optimize out this gather - addGSMetadata(call, currentPos); + if (disableGSWarningCount == 0) + addGSMetadata(call, currentPos); return BitCastInst(call, llvmReturnType, "gather_bitcast"); } @@ -2801,7 +2817,9 @@ FunctionEmitContext::scatter(llvm::Value *value, llvm::Value *ptr, args.push_back(value); args.push_back(mask); llvm::Value *inst = CallInst(scatterFunc, NULL, args); - addGSMetadata(inst, currentPos); + + if (disableGSWarningCount == 0) + addGSMetadata(inst, currentPos); } diff --git a/ctx.h b/ctx.h index 74b19596..0b1ccffa 100644 --- a/ctx.h +++ b/ctx.h @@ -230,6 +230,13 @@ public: 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; } /** Step through the code and find label statements; create a basic @@ -644,6 +651,10 @@ private: tasks launched from the current function. */ 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 labelMap; static bool initLabelBBlocks(ASTNode *node, void *data); diff --git a/opt.cpp b/opt.cpp index f2a9f154..af661d9a 100644 --- a/opt.cpp +++ b/opt.cpp @@ -2382,8 +2382,7 @@ GSToLoadStorePass::runOnBasicBlock(llvm::BasicBlock &bb) { continue; SourcePos pos; - bool ok = lGetSourcePosFromMetadata(callInst, &pos); - Assert(ok); + lGetSourcePosFromMetadata(callInst, &pos); llvm::Value *base = callInst->getArgOperand(0); llvm::Value *varyingOffsets = callInst->getArgOperand(1); @@ -2742,8 +2741,7 @@ static void lCoalescePerfInfo(const std::vector &coalesceGroup, const std::vector &loadOps) { SourcePos pos; - bool ok = lGetSourcePosFromMetadata(coalesceGroup[0], &pos); - Assert(ok); + lGetSourcePosFromMetadata(coalesceGroup[0], &pos); // Create a string that indicates the line numbers of the subsequent // gathers from the first one that were coalesced here. @@ -2758,12 +2756,13 @@ lCoalescePerfInfo(const std::vector &coalesceGroup, for (int i = 1; i < (int)coalesceGroup.size(); ++i) { SourcePos p; bool ok = lGetSourcePosFromMetadata(coalesceGroup[i], &p); - Assert(ok); - char buf[32]; - sprintf(buf, "%d", p.first_line); - strcat(otherPositions, buf); - if (i < (int)coalesceGroup.size() - 1) - strcat(otherPositions, ", "); + if (ok) { + char buf[32]; + sprintf(buf, "%d", p.first_line); + strcat(otherPositions, buf); + if (i < (int)coalesceGroup.size() - 1) + strcat(otherPositions, ", "); + } } strcat(otherPositions, ") "); } @@ -3460,8 +3459,7 @@ GatherCoalescePass::runOnBasicBlock(llvm::BasicBlock &bb) { continue; SourcePos pos; - bool ok = lGetSourcePosFromMetadata(callInst, &pos); - Assert(ok); + lGetSourcePosFromMetadata(callInst, &pos); Debug(pos, "Checking for coalescable gathers starting here..."); 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 // instruction so that we can issue PerformanceWarning()s below. SourcePos pos; - bool ok = lGetSourcePosFromMetadata(callInst, &pos); - Assert(ok); + bool gotPosition = lGetSourcePosFromMetadata(callInst, &pos); callInst->setCalledFunction(info->actualFunc); - if (g->target.vectorWidth > 1) { + if (gotPosition && g->target.vectorWidth > 1) { if (info->isGather) PerformanceWarning(pos, "Gather required to compute value in expression."); else diff --git a/stmt.cpp b/stmt.cpp index a9905096..363aa920 100644 --- a/stmt.cpp +++ b/stmt.cpp @@ -1745,7 +1745,9 @@ ForeachStmt::EmitCode(FunctionEmitContext *ctx) const { ctx->SetCurrentBasicBlock(bbMaskedBody); { ctx->AddInstrumentationPoint("foreach loop body (masked)"); ctx->SetContinueTarget(bbMaskedBodyContinue); + ctx->DisableGatherScatterWarnings(); stmts->EmitCode(ctx); + ctx->EnableGatherScatterWarnings(); ctx->BranchInst(bbMaskedBodyContinue); } ctx->SetCurrentBasicBlock(bbMaskedBodyContinue); {