RSInvariant.cpp revision 150eef4097c996e14b50b7f58184265385c44713
11d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross/*
21d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Copyright 2015, The Android Open Source Project
31d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
41d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Licensed under the Apache License, Version 2.0 (the "License");
51d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * you may not use this file except in compliance with the License.
61d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * You may obtain a copy of the License at
71d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
81d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *     http://www.apache.org/licenses/LICENSE-2.0
91d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
101d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Unless required by applicable law or agreed to in writing, software
111d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * distributed under the License is distributed on an "AS IS" BASIS,
121d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * See the License for the specific language governing permissions and
141d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * limitations under the License.
151d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross */
161d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
171d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include "bcc/Renderscript/RSTransforms.h"
181d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
191d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Function.h>
201d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Instructions.h>
211d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Metadata.h>
221d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Module.h>
231d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Type.h>
241d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/Pass.h>
251d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
261d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossnamespace {
271d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
281d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross/*
291d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * RSInvariantPass - This pass looks for Loads that access
301d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * RsExpandKernelDriverInfo instances (which should never be written by
311d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * a script, only by the driver) and marks them "invariant.load".
321d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
331d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * There should be only two sources of Loads from such instances:
341d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - An instance can appear as an argument of type
351d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   "RsExpandKernelDriverInfoPfx*" passed to a .expand function by
361d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   the driver.
371d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - An instance can appear as an argument of type
381d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   "rs_kernel_context_t*" passed to an API query function by the
391d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   user.
401d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Only the compiler-generated .expand functions and the API query
411d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * functions can see the fields of RsExpandKernelDriverInfo --
421d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * rs_kernel_context_t is opaque to user code, so there cannot be any
431d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Loads from it in user code.
441d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
451d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * This pass should be run
461d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - after foreachexp, so that it can see the Loads generated within
471d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   .expand functions
481d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - before inlining, so that it can recognize API query function
491d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   arguments.
501d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
511d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * WARNINGS:
521d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - If user code or APIs can modify RsExpandKernelDriverInfo
531d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   instances, this pass MAY ALLOW ILLEGAL OPTIMIZATION.
541d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - If this pass runs at a different time, it may be ineffective
551d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   (fail to mark some or all eligible Loads, and thereby cost
561d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   performance).
571d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - If the names of the data types change, this pass may be
581d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   ineffective.
591d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - If the patterns by which fields are loaded from
601d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   RsExpandKernelDriverInfo instances change, this pass may be
611d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   ineffective.
621d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross */
631d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossclass RSInvariantPass : public llvm::FunctionPass {
641d93a190e62ec1588b4724ca8759216b2d0b76d7David Grosspublic:
651d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  static char ID;
661d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
671d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  RSInvariantPass() : FunctionPass(ID), EmptyMDNode(nullptr) { }
681d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
691d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  virtual bool doInitialization(llvm::Module &M) {
701d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    EmptyMDNode = llvm::MDNode::get(M.getContext(), llvm::None);
711d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    return true;
721d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  }
731d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
741d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  virtual bool runOnFunction(llvm::Function &F) {
751d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    bool Changed = false;
761d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
771d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    for (llvm::Value &Arg : F.args()) {
781d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      const llvm::Type *ArgType = Arg.getType();
791d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      if (ArgType->isPointerTy()) {
801d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        const llvm::Type *ArgPtrDomainType =  ArgType->getPointerElementType();
81e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar        if (auto ArgPtrDomainStructType = llvm::dyn_cast<llvm::StructType>(ArgPtrDomainType)) {
82150eef4097c996e14b50b7f58184265385c44713Pirama Arumuga Nainar          if (!ArgPtrDomainStructType->isLiteral()) {
83e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar            const llvm::StringRef StructName = ArgPtrDomainStructType->getName();
84e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar            if (StructName.equals("struct.rs_kernel_context_t") || StructName.equals("RsExpandKernelDriverInfoPfx")) {
85e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar              Changed |= markInvariantUserLoads(&Arg);
86e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar            }
871d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross          }
881d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        }
891d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      }
901d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    }
911d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
921d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    return Changed;
931d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  }
941d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
951d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  virtual const char *getPassName() const {
961d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    return "Renderscript Invariant Load Annotation";
971d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  }
981d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
991d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossprivate:
1001d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1011d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  /*
1021d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * Follow def->use chains rooted at Value through calculations
1031d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * "based on" Value (see the "based on" definition at
1041d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * http://llvm.org/docs/LangRef.html#pointer-aliasing-rules).  If a
1051d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * chain reaches the pointer operand of a Load, mark that Load as
1061d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * "invariant.load" -- i.e., it accesses memory which does not
1071d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * change.
1081d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   */
1091d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  bool markInvariantUserLoads(llvm::Value *Value) {
1101d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    bool Changed = false;
1111d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    for (llvm::Use &Use : Value->uses()) {
1121d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      llvm::Instruction *Inst = llvm::cast<llvm::Instruction>(Use.getUser());
1131d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1141d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      /*
1151d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * We only examine a small set of opcodes here, because these
1161d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * are the opcodes that currently appear in the patterns of
1171d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * interest (foreachexp-generated code, and
1181d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * rsGet*(rs_kernel_context_t*) APIs).  Other opcodes could be
1191d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * added if necessary.
1201d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       */
1211d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      if (auto BitCast = llvm::dyn_cast<llvm::BitCastInst>(Inst)) {
1221d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        Changed |= markInvariantUserLoads(BitCast);
1231d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      } else if (auto GetElementPtr = llvm::dyn_cast<llvm::GetElementPtrInst>(Inst)) {
1241d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        if (Use.get() == GetElementPtr->getPointerOperand())
1251d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross          Changed |= markInvariantUserLoads(GetElementPtr);
1261d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      } else if (auto Load = llvm::dyn_cast<llvm::LoadInst>(Inst)) {
1271d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        if (Use.get() == Load->getPointerOperand()) {
1281d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross          Load->setMetadata("invariant.load", EmptyMDNode);
1291d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross          Changed = true;
1301d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        }
1311d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      }
1321d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    }
1331d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    return Changed;
1341d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  }
1351d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1361d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  // Pointer to empty metadata node used for "invariant.load" marking.
1371d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  llvm::MDNode *EmptyMDNode;
1381d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross}; // end RSInvariantPass
1391d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1401d93a190e62ec1588b4724ca8759216b2d0b76d7David Grosschar RSInvariantPass::ID = 0;
1411d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossllvm::RegisterPass<RSInvariantPass> X("rsinvariant", "RS Invariant Load Pass");
1421d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1431d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross} // end anonymous namespace
1441d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1451d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossnamespace bcc {
1461d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1471d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossllvm::FunctionPass *
1481d93a190e62ec1588b4724ca8759216b2d0b76d7David GrosscreateRSInvariantPass() {
1491d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  return new RSInvariantPass();
1501d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross}
1511d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1521d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross} // end namespace bcc
153