-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick-llvm
324 lines (270 loc) · 9.78 KB
/
brick-llvm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* (c) 2018 Henrich Lauko
* (c) 2014-2018 Vladimír Štill
* (c) 2014, 2015, 2022 Petr Ročkai
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#pragma once
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Object/IRObjectFile.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/raw_os_ostream.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/IR/AbstractCallSite.h>
#include <memory>
#include <brick-assert>
#include <brick-string>
#include <brick-types>
#include <brick-fs>
namespace brq
{
template< typename T >
using get_operand_t = decltype( std::declval<T&>().getOperand( std::declval< int >() ) );
template< typename value_t >
struct llvm_transform
{
using value_type = std::conditional_t< std::is_pointer_v< value_t >, value_t, value_t * >;
constexpr llvm_transform( const value_type &val ) : state( val ) {}
constexpr llvm_transform( value_type &&val ) : state( std::move( val ) ) {}
template< typename fun_t >
constexpr decltype( auto ) apply( fun_t f ) noexcept
{
using result_t = decltype( f( state ) );
if ( state )
return llvm_transform< result_t >( f( state ) );
else
return llvm_transform< result_t >( nullptr );
}
constexpr decltype( auto ) operand( size_t idx ) noexcept
{
if constexpr ( brick::types::is_detected_v< get_operand_t, std::remove_pointer_t< value_t > > )
return apply( [idx] ( const auto& v ) { return v->getOperand( idx ); } );
else
return llvm_transform< value_type >( nullptr );
}
template< typename T >
constexpr decltype( auto ) cast() noexcept
{
return apply( [] ( const auto& v ) { return ::llvm::dyn_cast< T >( v ); } );
}
constexpr value_type freeze() noexcept { return state; }
private:
value_type state;
};
template< typename value_t >
llvm_transform( value_t val ) -> llvm_transform< value_t >;
struct llvm_annotation : brick::types::Eq
{
llvm_annotation() = default;
explicit llvm_annotation( std::string_view anno )
{
size_t oldoff = 0, off = 0;
do {
off = anno.find( '.', oldoff );
_parts.emplace_back( anno.substr( oldoff, off - oldoff ) );
oldoff = off + 1;
} while ( off != std::string::npos );
}
template< typename iter_t >
llvm_annotation( iter_t begin, iter_t end ) : _parts( begin, end ) {}
std::string name() const { return _parts.back(); }
llvm_annotation ns() const { return llvm_annotation( _parts.begin(), _parts.end() - 1 ); }
std::string to_string() const
{
brq::string_builder ss;
for ( auto &n : _parts )
ss << n << ".";
return std::string( ss.data(), 0, ss.data().size() - 1 );
}
bool in_namespace( llvm_annotation ns ) const
{
return ns._parts.size() < _parts.size()
&& std::equal( ns._parts.begin(), ns._parts.end(), _parts.begin() );
}
llvm_annotation drop_namespace( llvm_annotation ns ) const
{
return in_namespace( ns )
? llvm_annotation( _parts.begin() + ns.size(), _parts.end() )
: *this;
}
size_t size() const { return _parts.size(); }
bool operator==( const llvm_annotation &o ) const
{
return o.size() == size() && std::equal( _parts.begin(), _parts.end(), o._parts.begin() );
}
private:
std::vector< std::string > _parts;
};
template< typename source_t = llvm::Module *, typename view_t = brq::unit >
struct llvm_enumerate
{
static_assert( !std::is_same_v< source_t, llvm::Module * > ||
std::is_same_v< view_t, brq::unit > );
source_t src;
view_t view;
llvm_enumerate( const source_t &src ) : src( src ) {}
llvm_enumerate( const source_t &src, const view_t &view ) : src( src ), view( view ) {}
template< typename yield_t >
void operator>>( yield_t &&yield )
{
if constexpr ( std::is_same_v< source_t, llvm::Module * > )
fetch( yield );
else
src >> [&]( auto... xs ) { view( xs..., yield ); };
}
template< typename yield_t >
void fetch( yield_t &&yield )
{
static_assert( std::is_same_v< source_t, llvm::Module * > );
const auto annos = src->getNamedGlobal( "llvm.global.annotations" );
auto arr = llvm_transform( annos ).operand( 0 ).template cast< llvm::ConstantArray >().freeze();
if ( arr )
for ( const auto &op : arr->operands() )
{
auto cs = llvm_transform( op.get() ).template cast< llvm::ConstantStruct >();
if ( auto val = cs.operand( 0 ).operand( 0 ).freeze() )
{
auto anno = cs.operand( 1 ).operand( 0 )
.template cast< llvm::GlobalVariable >().operand( 0 )
.template cast< llvm::ConstantDataArray >()
.freeze();
yield( val, llvm_annotation( anno->getAsCString().str() ) );
}
}
}
template< typename add_view_t >
auto operator|( add_view_t av )
{
return brq::llvm_enumerate( *this, av );
}
template< typename value_t >
auto as()
{
return *this | []( auto val, auto anno, auto yield )
{
yield( llvm::cast< value_t >( val ), anno );
};
}
auto in_namespace( llvm_annotation ns )
{
return *this | [=]( const auto &val, llvm_annotation anno, auto yield )
{
if ( anno.in_namespace( ns ) )
yield( val, anno.drop_namespace( ns ) );
};
}
auto equal( llvm_annotation match )
{
return *this | [=]( const auto &val, llvm_annotation anno, auto yield )
{
if ( match == anno )
yield( val );
};
}
auto in_namespace( std::string_view ns ) { return in_namespace( llvm_annotation( ns ) ); }
auto equal( std::string_view anno ) { return equal( llvm_annotation( anno ) ); }
auto as_function() { return as< llvm::Function >(); }
auto functions( std::string_view anno ) { return as_function().equal( anno ); }
};
template< typename yield_t >
void llvm_functions_by_attribute( const std::string& attr, ::llvm::Module &m, yield_t &&yield )
{
for ( auto & fn : m )
if ( fn.hasFnAttribute( attr ) )
yield( fn );
}
inline void llvm_verify( llvm::Module *module )
{
std::string err;
::llvm::raw_string_ostream serr( err );
if ( llvm::verifyModule( *module, &serr ) )
throw std::runtime_error( "Invalid bitcode: " + serr.str() );
}
inline void llvm_throw( std::error_code ec )
{
throw std::runtime_error( "LLVM Error: " + ec.message() );
}
inline void llvm_throw( ::llvm::Error &e )
{
throw std::runtime_error( "LLVM Error: " + toString( std::move( e ) ) );
}
inline void llvm_throw( ::llvm::Error &&e )
{
throw std::runtime_error( "LLVM Error: " + toString( std::move( e ) ) );
}
inline void llvm_write( llvm::Module *m, std::string path )
{
llvm_verify( m );
std::error_code serr;
brq::create_file( path );
llvm::raw_fd_ostream fs( path.c_str(), serr, llvm::sys::fs::OF_None );
if ( serr )
llvm_throw( serr );
WriteBitcodeToFile( *m, fs );
}
inline std::unique_ptr< llvm::Module > llvm_read( const std::string &str, llvm::LLVMContext *ctx )
{
std::unique_ptr< llvm::MemoryBuffer > input = std::move( llvm::MemoryBuffer::getFile( str ).get() );
auto bc_input = llvm::object::IRObjectFile::findBitcodeInMemBuffer( input->getMemBufferRef() );
if ( !bc_input )
UNREACHABLE( "Could not load bitcode file" );
if ( auto mod = ::llvm::parseBitcodeFile( bc_input.get(), *ctx ) )
return std::move( mod.get() );
else
UNREACHABLE( "Error parsing input model; probably not a valid bc file." );
}
inline std::string llvm_serialize( llvm::Module *m )
{
std::string s;
llvm_verify( m );
{
::llvm::raw_string_ostream fs( s );
WriteBitcodeToFile( *m, fs );
}
return s;
}
inline int llvm_intrinsic_id( llvm::Value *v )
{
auto insn = llvm::dyn_cast< llvm::Instruction >( v );
if ( !insn || insn->getOpcode() != llvm::Instruction::Call )
return llvm::Intrinsic::not_intrinsic;
auto *CB = llvm::cast< llvm::CallBase >( insn );
auto f = CB->getCalledFunction();
if ( !f )
return llvm::Intrinsic::not_intrinsic;
return f->getIntrinsicID();
}
}
namespace llvm
{
template< typename stream, typename T >
auto operator<<( stream &o, const T &v )
-> std::enable_if_t< !std::is_convertible_v< stream &, raw_ostream & > &&
!std::is_convertible_v< T, std::string >,
decltype( v.print( std::declval< raw_ostream & >() ), o ) >
{
std::string s;
raw_string_ostream lstream( s );
lstream << v;
o << lstream.str();
return o;
}
}