Hi Stephan,> intrinsic float4 sample(int tex, float2 tc); > > float4 main(int tex, float2 tc) > { > float4 x = sample(tex, tc); > return 0.0; > }without additional information it would be wrong to remove the call to sample because it might write to a global variable.> As you can see, the call to the sample function is still present, > although the actual value it is supposed to return via its sret > parameter is never used.Quite right too, see above.> Using the AAEvalPass I found out that the alias analysis pass I > implemented seems to work alright (it reports mod for %5): > > ===== Alias Analysis Evaluator Report ====> 3 Total Alias Queries Performed > 3 no alias responses (100.0%) > 0 may alias responses (0.0%) > 0 must alias responses (0.0%) > Alias Analysis Evaluator Pointer Alias Summary: 100%/0%/0% > 3 Total ModRef Queries Performed > 2 no mod/ref responses (66.6%) > 1 mod responses (33.3%) > 0 ref responses (0.0%) > 0 mod & ref responses (0.0%) > Alias Analysis Evaluator Mod/Ref Summary: 66%/33%/0%/0% > > Yet, DCE, DSE and GVN fail to remove the function call. (I'm not so > sure which optimization pass to use, so I picked these three as they > seemed to make sense.)In order to perform this transform the optimizers would have to work out that sample does not modify any global state. This cannot be done without knowing the definition of sample, but you only provide a declaration. If you provided a body too then the GlobalsModRef analysis might be able to work it out. Ciao, Duncan.
Duncan, thanks for your answer!> In order to perform this transform the optimizers would have to work out > that sample does not modify any global state. This cannot be done without > knowing the definition of sample, but you only provide a declaration.Which is why I am trying to supply this additional information in a custom alias analysis pass, but it doesn't seem to work. (The AAEvalPass stats are precisely for this custom pass.) Could you take a look at the code, please? Am I missing something here? class VISIBILITY_HIDDEN MySretAliasAnalysis : public FunctionPass, public AliasAnalysis { std::map<std::string, bool> _srets; public: static char ID; MySretAliasAnalysis() : FunctionPass(&ID) { _srets["sample$int$float2"] = true; _srets["sample$int$float3"] = true; } void getAnalysisUsage(llvm::AnalysisUsage &usage) const { AliasAnalysis::getAnalysisUsage(usage); usage.setPreservesAll(); } bool runOnFunction(Function &F) { AliasAnalysis::InitializeAliasAnalysis(this); return false; } ModRefBehavior getModRefBehavior(CallSite CS, std::vector<PointerAccessInfo> *Info = 0) { if(_srets.find(CS.getCalledFunction()->getName()) != _srets.end()) return AliasAnalysis::AccessesArguments; // only accesses args, no globals return AliasAnalysis::getModRefBehavior(CS, Info); } ModRefBehavior getModRefBehavior(Function *F, std::vector<PointerAccessInfo> *Info = 0) { if(_srets.find(F->getName()) != _srets.end()) return AliasAnalysis::AccessesArguments; // only accesses args, no globals return AliasAnalysis::getModRefBehavior(F, Info); } ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) { std::string functionName = CS.getCalledFunction()->getNameStr(); if(_srets.find(functionName) != _srets.end()) { if(CS.hasArgument(P)) { if(CS.getArgument(0) == P) return AliasAnalysis::Mod; // modify value pointed to by sret param else return AliasAnalysis::NoModRef; // there aren't any other pointer args } } return AliasAnalysis::getModRefInfo(CS, P, Size); } bool hasNoModRefInfoForCalls() const { return false; } };> If you provided a body too then the GlobalsModRef analysis might be able to > work it out.That's not an option because I want the sample function to be resolved as an external function by the jitter. Thanks for your time, Stephan
Hi Stephan,>> In order to perform this transform the optimizers would have to work out >> that sample does not modify any global state. This cannot be done without >> knowing the definition of sample, but you only provide a declaration. > > Which is why I am trying to supply this additional information in a > custom alias analysis pass, but it doesn't seem to work. (The > AAEvalPass stats are precisely for this custom pass.) > > Could you take a look at the code, please? Am I missing something > here?I think this cannot possibly work. Imagine that I am a pass trying to determine if the function call modifies global state. What would I have to do? I would have to consider all global variables, and query alias analysis to find out if any of them are modified. But how can I know all global variables? There is no way to know about global variables that are not declared in the module, so I can't query about those. And even if I could know about all global variables, checking all of them every time I want to consider deleting a function call would be very expensive and not worthwhile in general. GlobalsModRef only considers global variables with internal linkage IIRC (I may be wrong about this). That said, maybe there is some special AA query method that I don't know about that asks "do you write any global state". Ciao, Duncan.