1 //===- IRTransformLayer.h - Run all IR through a functor --------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Run all IR passed in through a user supplied functor. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H 14 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H 15 16 #include "llvm/ADT/FunctionExtras.h" 17 #include "llvm/ExecutionEngine/JITSymbol.h" 18 #include "llvm/ExecutionEngine/Orc/Layer.h" 19 #include <memory> 20 21 namespace llvm { 22 class Module; 23 namespace orc { 24 25 /// A layer that applies a transform to emitted modules. 26 /// The transform function is responsible for locking the ThreadSafeContext 27 /// before operating on the module. 28 class IRTransformLayer : public IRLayer { 29 public: 30 using TransformFunction = unique_function<Expected<ThreadSafeModule>( 31 ThreadSafeModule, MaterializationResponsibility &R)>; 32 33 IRTransformLayer(ExecutionSession &ES, IRLayer &BaseLayer, 34 TransformFunction Transform = identityTransform); 35 36 void setTransform(TransformFunction Transform) { 37 this->Transform = std::move(Transform); 38 } 39 40 void emit(std::unique_ptr<MaterializationResponsibility> R, 41 ThreadSafeModule TSM) override; 42 43 static ThreadSafeModule identityTransform(ThreadSafeModule TSM, 44 MaterializationResponsibility &R) { 45 return TSM; 46 } 47 48 private: 49 IRLayer &BaseLayer; 50 TransformFunction Transform; 51 }; 52 53 } // end namespace orc 54 } // end namespace llvm 55 56 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H 57