gallivm: Use standard LLVMSetAlignment from LLVM 3.4 onwards.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_misc.cpp
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 /**
30 * The purpose of this module is to expose LLVM functionality not available
31 * through the C++ bindings.
32 */
33
34
35 #ifndef __STDC_LIMIT_MACROS
36 #define __STDC_LIMIT_MACROS
37 #endif
38
39 #ifndef __STDC_CONSTANT_MACROS
40 #define __STDC_CONSTANT_MACROS
41 #endif
42
43 // Undef these vars just to silence warnings
44 #undef PACKAGE_BUGREPORT
45 #undef PACKAGE_NAME
46 #undef PACKAGE_STRING
47 #undef PACKAGE_TARNAME
48 #undef PACKAGE_VERSION
49
50
51 #include <stddef.h>
52
53 // Workaround http://llvm.org/PR23628
54 #if HAVE_LLVM >= 0x0307
55 # pragma push_macro("DEBUG")
56 # undef DEBUG
57 #endif
58
59 #include <llvm-c/Core.h>
60 #include <llvm-c/ExecutionEngine.h>
61 #include <llvm/Target/TargetOptions.h>
62 #include <llvm/ExecutionEngine/ExecutionEngine.h>
63 #include <llvm/ADT/Triple.h>
64 #if HAVE_LLVM >= 0x0307
65 #include <llvm/Analysis/TargetLibraryInfo.h>
66 #else
67 #include <llvm/Target/TargetLibraryInfo.h>
68 #endif
69 #if HAVE_LLVM < 0x0306
70 #include <llvm/ExecutionEngine/JITMemoryManager.h>
71 #else
72 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
73 #endif
74 #include <llvm/Support/CommandLine.h>
75 #include <llvm/Support/Host.h>
76 #include <llvm/Support/PrettyStackTrace.h>
77
78 #include <llvm/Support/TargetSelect.h>
79
80 #include <llvm/IR/IRBuilder.h>
81 #include <llvm/IR/Module.h>
82 #include <llvm/Support/CBindingWrapping.h>
83
84 // Workaround http://llvm.org/PR23628
85 #if HAVE_LLVM >= 0x0307
86 # pragma pop_macro("DEBUG")
87 #endif
88
89 #include "c11/threads.h"
90 #include "os/os_thread.h"
91 #include "pipe/p_config.h"
92 #include "util/u_debug.h"
93 #include "util/u_cpu_detect.h"
94
95 #include "lp_bld_misc.h"
96
97 namespace {
98
99 class LLVMEnsureMultithreaded {
100 public:
101 LLVMEnsureMultithreaded()
102 {
103 if (!LLVMIsMultithreaded()) {
104 LLVMStartMultithreaded();
105 }
106 }
107 };
108
109 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
110
111 }
112
113 static once_flag init_native_targets_once_flag;
114
115 static void init_native_targets()
116 {
117 // If we have a native target, initialize it to ensure it is linked in and
118 // usable by the JIT.
119 llvm::InitializeNativeTarget();
120
121 llvm::InitializeNativeTargetAsmPrinter();
122
123 llvm::InitializeNativeTargetDisassembler();
124 }
125
126 /**
127 * The llvm target registry is not thread-safe, so drivers and state-trackers
128 * that want to initialize targets should use the gallivm_init_llvm_targets()
129 * function to safely initialize targets.
130 *
131 * LLVM targets should be initialized before the driver or state-tracker tries
132 * to access the registry.
133 */
134 extern "C" void
135 gallivm_init_llvm_targets(void)
136 {
137 call_once(&init_native_targets_once_flag, init_native_targets);
138 }
139
140 extern "C" void
141 lp_set_target_options(void)
142 {
143 #if HAVE_LLVM < 0x0304
144 /*
145 * By default LLVM adds a signal handler to output a pretty stack trace.
146 * This signal handler is never removed, causing problems when unloading the
147 * shared object where the gallium driver resides.
148 */
149 llvm::DisablePrettyStackTrace = true;
150 #endif
151
152 gallivm_init_llvm_targets();
153 }
154
155 extern "C"
156 LLVMTargetLibraryInfoRef
157 gallivm_create_target_library_info(const char *triple)
158 {
159 return reinterpret_cast<LLVMTargetLibraryInfoRef>(
160 #if HAVE_LLVM < 0x0307
161 new llvm::TargetLibraryInfo(
162 #else
163 new llvm::TargetLibraryInfoImpl(
164 #endif
165 llvm::Triple(triple)));
166 }
167
168 extern "C"
169 void
170 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info)
171 {
172 delete reinterpret_cast<
173 #if HAVE_LLVM < 0x0307
174 llvm::TargetLibraryInfo
175 #else
176 llvm::TargetLibraryInfoImpl
177 #endif
178 *>(library_info);
179 }
180
181 extern "C"
182 LLVMValueRef
183 lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
184 const char *Name)
185 {
186 return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
187 }
188
189
190 #if HAVE_LLVM < 0x0304
191
192 extern "C"
193 void
194 LLVMSetAlignmentBackport(LLVMValueRef V,
195 unsigned Bytes)
196 {
197 switch (LLVMGetInstructionOpcode(V)) {
198 case LLVMLoad:
199 llvm::unwrap<llvm::LoadInst>(V)->setAlignment(Bytes);
200 break;
201 case LLVMStore:
202 llvm::unwrap<llvm::StoreInst>(V)->setAlignment(Bytes);
203 break;
204 default:
205 assert(0);
206 break;
207 }
208 }
209
210 #endif
211
212
213 #if HAVE_LLVM < 0x0306
214 typedef llvm::JITMemoryManager BaseMemoryManager;
215 #else
216 typedef llvm::RTDyldMemoryManager BaseMemoryManager;
217 #endif
218
219
220 /*
221 * Delegating is tedious but the default manager class is hidden in an
222 * anonymous namespace in LLVM, so we cannot just derive from it to change
223 * its behavior.
224 */
225 class DelegatingJITMemoryManager : public BaseMemoryManager {
226
227 protected:
228 virtual BaseMemoryManager *mgr() const = 0;
229
230 public:
231 #if HAVE_LLVM < 0x0306
232 /*
233 * From JITMemoryManager
234 */
235 virtual void setMemoryWritable() {
236 mgr()->setMemoryWritable();
237 }
238 virtual void setMemoryExecutable() {
239 mgr()->setMemoryExecutable();
240 }
241 virtual void setPoisonMemory(bool poison) {
242 mgr()->setPoisonMemory(poison);
243 }
244 virtual void AllocateGOT() {
245 mgr()->AllocateGOT();
246 /*
247 * isManagingGOT() is not virtual in base class so we can't delegate.
248 * Instead we mirror the value of HasGOT in our instance.
249 */
250 HasGOT = mgr()->isManagingGOT();
251 }
252 virtual uint8_t *getGOTBase() const {
253 return mgr()->getGOTBase();
254 }
255 virtual uint8_t *startFunctionBody(const llvm::Function *F,
256 uintptr_t &ActualSize) {
257 return mgr()->startFunctionBody(F, ActualSize);
258 }
259 virtual uint8_t *allocateStub(const llvm::GlobalValue *F,
260 unsigned StubSize,
261 unsigned Alignment) {
262 return mgr()->allocateStub(F, StubSize, Alignment);
263 }
264 virtual void endFunctionBody(const llvm::Function *F,
265 uint8_t *FunctionStart,
266 uint8_t *FunctionEnd) {
267 mgr()->endFunctionBody(F, FunctionStart, FunctionEnd);
268 }
269 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
270 return mgr()->allocateSpace(Size, Alignment);
271 }
272 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
273 return mgr()->allocateGlobal(Size, Alignment);
274 }
275 virtual void deallocateFunctionBody(void *Body) {
276 mgr()->deallocateFunctionBody(Body);
277 }
278 #if HAVE_LLVM < 0x0304
279 virtual uint8_t *startExceptionTable(const llvm::Function *F,
280 uintptr_t &ActualSize) {
281 return mgr()->startExceptionTable(F, ActualSize);
282 }
283 virtual void endExceptionTable(const llvm::Function *F,
284 uint8_t *TableStart,
285 uint8_t *TableEnd,
286 uint8_t *FrameRegister) {
287 mgr()->endExceptionTable(F, TableStart, TableEnd,
288 FrameRegister);
289 }
290 virtual void deallocateExceptionTable(void *ET) {
291 mgr()->deallocateExceptionTable(ET);
292 }
293 #endif
294 virtual bool CheckInvariants(std::string &s) {
295 return mgr()->CheckInvariants(s);
296 }
297 virtual size_t GetDefaultCodeSlabSize() {
298 return mgr()->GetDefaultCodeSlabSize();
299 }
300 virtual size_t GetDefaultDataSlabSize() {
301 return mgr()->GetDefaultDataSlabSize();
302 }
303 virtual size_t GetDefaultStubSlabSize() {
304 return mgr()->GetDefaultStubSlabSize();
305 }
306 virtual unsigned GetNumCodeSlabs() {
307 return mgr()->GetNumCodeSlabs();
308 }
309 virtual unsigned GetNumDataSlabs() {
310 return mgr()->GetNumDataSlabs();
311 }
312 virtual unsigned GetNumStubSlabs() {
313 return mgr()->GetNumStubSlabs();
314 }
315 #endif
316
317 /*
318 * From RTDyldMemoryManager
319 */
320 #if HAVE_LLVM >= 0x0304
321 virtual uint8_t *allocateCodeSection(uintptr_t Size,
322 unsigned Alignment,
323 unsigned SectionID,
324 llvm::StringRef SectionName) {
325 return mgr()->allocateCodeSection(Size, Alignment, SectionID,
326 SectionName);
327 }
328 #else
329 virtual uint8_t *allocateCodeSection(uintptr_t Size,
330 unsigned Alignment,
331 unsigned SectionID) {
332 return mgr()->allocateCodeSection(Size, Alignment, SectionID);
333 }
334 #endif
335 virtual uint8_t *allocateDataSection(uintptr_t Size,
336 unsigned Alignment,
337 unsigned SectionID,
338 #if HAVE_LLVM >= 0x0304
339 llvm::StringRef SectionName,
340 #endif
341 bool IsReadOnly) {
342 return mgr()->allocateDataSection(Size, Alignment, SectionID,
343 #if HAVE_LLVM >= 0x0304
344 SectionName,
345 #endif
346 IsReadOnly);
347 }
348 #if HAVE_LLVM >= 0x0304
349 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
350 mgr()->registerEHFrames(Addr, LoadAddr, Size);
351 }
352 virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
353 mgr()->deregisterEHFrames(Addr, LoadAddr, Size);
354 }
355 #else
356 virtual void registerEHFrames(llvm::StringRef SectionData) {
357 mgr()->registerEHFrames(SectionData);
358 }
359 #endif
360 virtual void *getPointerToNamedFunction(const std::string &Name,
361 bool AbortOnFailure=true) {
362 return mgr()->getPointerToNamedFunction(Name, AbortOnFailure);
363 }
364 #if HAVE_LLVM <= 0x0303
365 virtual bool applyPermissions(std::string *ErrMsg = 0) {
366 return mgr()->applyPermissions(ErrMsg);
367 }
368 #else
369 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
370 return mgr()->finalizeMemory(ErrMsg);
371 }
372 #endif
373 };
374
375
376 /*
377 * Delegate memory management to one shared manager for more efficient use
378 * of memory than creating a separate pool for each LLVM engine.
379 * Keep generated code until freeGeneratedCode() is called, instead of when
380 * memory manager is destroyed, which happens during engine destruction.
381 * This allows additional memory savings as we don't have to keep the engine
382 * around in order to use the code.
383 * All methods are delegated to the shared manager except destruction and
384 * deallocating code. For the latter we just remember what needs to be
385 * deallocated later. The shared manager is deleted once it is empty.
386 */
387 class ShaderMemoryManager : public DelegatingJITMemoryManager {
388
389 BaseMemoryManager *TheMM;
390
391 struct GeneratedCode {
392 typedef std::vector<void *> Vec;
393 Vec FunctionBody, ExceptionTable;
394 BaseMemoryManager *TheMM;
395
396 GeneratedCode(BaseMemoryManager *MM) {
397 TheMM = MM;
398 }
399
400 ~GeneratedCode() {
401 /*
402 * Deallocate things as previously requested and
403 * free shared manager when no longer used.
404 */
405 #if HAVE_LLVM < 0x0306
406 Vec::iterator i;
407
408 assert(TheMM);
409 for ( i = FunctionBody.begin(); i != FunctionBody.end(); ++i )
410 TheMM->deallocateFunctionBody(*i);
411 #if HAVE_LLVM < 0x0304
412 for ( i = ExceptionTable.begin(); i != ExceptionTable.end(); ++i )
413 TheMM->deallocateExceptionTable(*i);
414 #endif /* HAVE_LLVM < 0x0304 */
415 #endif /* HAVE_LLVM < 0x0306 */
416 }
417 };
418
419 GeneratedCode *code;
420
421 BaseMemoryManager *mgr() const {
422 return TheMM;
423 }
424
425 public:
426
427 ShaderMemoryManager(BaseMemoryManager* MM) {
428 TheMM = MM;
429 code = new GeneratedCode(MM);
430 }
431
432 virtual ~ShaderMemoryManager() {
433 /*
434 * 'code' is purposely not deleted. It is the user's responsibility
435 * to call getGeneratedCode() and freeGeneratedCode().
436 */
437 }
438
439 struct lp_generated_code *getGeneratedCode() {
440 return (struct lp_generated_code *) code;
441 }
442
443 static void freeGeneratedCode(struct lp_generated_code *code) {
444 delete (GeneratedCode *) code;
445 }
446
447 #if HAVE_LLVM < 0x0304
448 virtual void deallocateExceptionTable(void *ET) {
449 // remember for later deallocation
450 code->ExceptionTable.push_back(ET);
451 }
452 #endif
453
454 virtual void deallocateFunctionBody(void *Body) {
455 // remember for later deallocation
456 code->FunctionBody.push_back(Body);
457 }
458 };
459
460
461 /**
462 * Same as LLVMCreateJITCompilerForModule, but:
463 * - allows using MCJIT and enabling AVX feature where available.
464 * - set target options
465 *
466 * See also:
467 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
468 * - llvm/tools/lli/lli.cpp
469 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
470 */
471 extern "C"
472 LLVMBool
473 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
474 lp_generated_code **OutCode,
475 LLVMModuleRef M,
476 LLVMMCJITMemoryManagerRef CMM,
477 unsigned OptLevel,
478 int useMCJIT,
479 char **OutError)
480 {
481 using namespace llvm;
482
483 std::string Error;
484 #if HAVE_LLVM >= 0x0306
485 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
486 #else
487 EngineBuilder builder(unwrap(M));
488 #endif
489
490 /**
491 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
492 * friends for configuring code generation options, like stack alignment.
493 */
494 TargetOptions options;
495 #if defined(PIPE_ARCH_X86)
496 options.StackAlignmentOverride = 4;
497 #if HAVE_LLVM < 0x0304
498 options.RealignStack = true;
499 #endif
500 #endif
501
502 #if defined(DEBUG) && HAVE_LLVM < 0x0307
503 options.JITEmitDebugInfo = true;
504 #endif
505
506 /* XXX: Workaround http://llvm.org/PR21435 */
507 #if defined(DEBUG) || defined(PROFILE) || \
508 (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)))
509 #if HAVE_LLVM < 0x0304
510 options.NoFramePointerElimNonLeaf = true;
511 #endif
512 #if HAVE_LLVM < 0x0307
513 options.NoFramePointerElim = true;
514 #endif
515 #endif
516
517 builder.setEngineKind(EngineKind::JIT)
518 .setErrorStr(&Error)
519 .setTargetOptions(options)
520 .setOptLevel((CodeGenOpt::Level)OptLevel);
521
522 if (useMCJIT) {
523 #if HAVE_LLVM < 0x0306
524 builder.setUseMCJIT(true);
525 #endif
526 #ifdef _WIN32
527 /*
528 * MCJIT works on Windows, but currently only through ELF object format.
529 */
530 std::string targetTriple = llvm::sys::getProcessTriple();
531 targetTriple.append("-elf");
532 unwrap(M)->setTargetTriple(targetTriple);
533 #endif
534 }
535
536 llvm::SmallVector<std::string, 16> MAttrs;
537
538 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
539 /*
540 * We need to unset attributes because sometimes LLVM mistakenly assumes
541 * certain features are present given the processor name.
542 *
543 * https://bugs.freedesktop.org/show_bug.cgi?id=92214
544 * http://llvm.org/PR25021
545 * http://llvm.org/PR19429
546 * http://llvm.org/PR16721
547 */
548 MAttrs.push_back(util_cpu_caps.has_sse ? "+sse" : "-sse" );
549 MAttrs.push_back(util_cpu_caps.has_sse2 ? "+sse2" : "-sse2" );
550 MAttrs.push_back(util_cpu_caps.has_sse3 ? "+sse3" : "-sse3" );
551 MAttrs.push_back(util_cpu_caps.has_ssse3 ? "+ssse3" : "-ssse3" );
552 #if HAVE_LLVM >= 0x0304
553 MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse4.1" : "-sse4.1");
554 #else
555 MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse41" : "-sse41" );
556 #endif
557 #if HAVE_LLVM >= 0x0304
558 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse4.2" : "-sse4.2");
559 #else
560 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse42" : "-sse42" );
561 #endif
562 /*
563 * AVX feature is not automatically detected from CPUID by the X86 target
564 * yet, because the old (yet default) JIT engine is not capable of
565 * emitting the opcodes. On newer llvm versions it is and at least some
566 * versions (tested with 3.3) will emit avx opcodes without this anyway.
567 */
568 MAttrs.push_back(util_cpu_caps.has_avx ? "+avx" : "-avx");
569 MAttrs.push_back(util_cpu_caps.has_f16c ? "+f16c" : "-f16c");
570 MAttrs.push_back(util_cpu_caps.has_avx2 ? "+avx2" : "-avx2");
571 #endif
572
573 #if defined(PIPE_ARCH_PPC)
574 MAttrs.push_back(util_cpu_caps.has_altivec ? "+altivec" : "-altivec");
575 #if HAVE_LLVM >= 0x0304
576 /*
577 * Make sure VSX instructions are disabled
578 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7
579 */
580 if (util_cpu_caps.has_altivec) {
581 MAttrs.push_back("-vsx");
582 }
583 #endif
584 #endif
585
586 builder.setMAttrs(MAttrs);
587
588 #if HAVE_LLVM >= 0x0305
589 StringRef MCPU = llvm::sys::getHostCPUName();
590 /*
591 * The cpu bits are no longer set automatically, so need to set mcpu manually.
592 * Note that the MAttrs set above will be sort of ignored (since we should
593 * not set any which would not be set by specifying the cpu anyway).
594 * It ought to be safe though since getHostCPUName() should include bits
595 * not only from the cpu but environment as well (for instance if it's safe
596 * to use avx instructions which need OS support). According to
597 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
598 * right it may be necessary to specify older cpu (or disable mattrs) though
599 * when not using MCJIT so no instructions are generated which the old JIT
600 * can't handle. Not entirely sure if we really need to do anything yet.
601 */
602 builder.setMCPU(MCPU);
603 #endif
604
605 ShaderMemoryManager *MM = NULL;
606 if (useMCJIT) {
607 #if HAVE_LLVM > 0x0303
608 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
609 MM = new ShaderMemoryManager(JMM);
610 *OutCode = MM->getGeneratedCode();
611
612 #if HAVE_LLVM >= 0x0306
613 builder.setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>(MM));
614 MM = NULL; // ownership taken by std::unique_ptr
615 #else
616 builder.setMCJITMemoryManager(MM);
617 #endif
618 #endif
619 } else {
620 #if HAVE_LLVM < 0x0306
621 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
622 MM = new ShaderMemoryManager(JMM);
623 *OutCode = MM->getGeneratedCode();
624
625 builder.setJITMemoryManager(MM);
626 #else
627 assert(0);
628 #endif
629 }
630
631 ExecutionEngine *JIT;
632
633 JIT = builder.create();
634 if (JIT) {
635 *OutJIT = wrap(JIT);
636 return 0;
637 }
638 lp_free_generated_code(*OutCode);
639 *OutCode = 0;
640 delete MM;
641 *OutError = strdup(Error.c_str());
642 return 1;
643 }
644
645
646 extern "C"
647 void
648 lp_free_generated_code(struct lp_generated_code *code)
649 {
650 ShaderMemoryManager::freeGeneratedCode(code);
651 }
652
653 extern "C"
654 LLVMMCJITMemoryManagerRef
655 lp_get_default_memory_manager()
656 {
657 BaseMemoryManager *mm;
658 #if HAVE_LLVM < 0x0306
659 mm = llvm::JITMemoryManager::CreateDefaultMemManager();
660 #else
661 mm = new llvm::SectionMemoryManager();
662 #endif
663 return reinterpret_cast<LLVMMCJITMemoryManagerRef>(mm);
664 }
665
666 extern "C"
667 void
668 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr)
669 {
670 delete reinterpret_cast<BaseMemoryManager*>(memorymgr);
671 }