Hash :
869dde72
Author :
Date :
2025-02-07T15:44:12
Remove code paths for invoking the offline Metal compiler We experimented with invoking the Metal shader compiler directly to create cacheable shader libraries but the project did not show useful gains. It is currently all dead code, remove it. Bug: b/391990604 Change-Id: I6d13278d1a27d8b3ae961a0846f31292f2506b23 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6245258 Reviewed-by: Kimmo Kinnunen <kkinnunen@apple.com> Commit-Queue: Geoff Lang <geofflang@chromium.org>
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
//
// Copyright 2023 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// mtl_library_cache.mm:
// Defines classes for caching of mtl libraries
//
#include "libANGLE/renderer/metal/mtl_library_cache.h"
#include <stdio.h>
#include <limits>
#include "common/MemoryBuffer.h"
#include "common/angleutils.h"
#include "common/hash_utils.h"
#include "common/mathutil.h"
#include "common/string_utils.h"
#include "common/system_utils.h"
#include "libANGLE/histogram_macros.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
#include "platform/PlatformMethods.h"
namespace rx
{
namespace mtl
{
LibraryCache::LibraryCache() : mCache(kMaxCachedLibraries) {}
angle::ObjCPtr<id<MTLLibrary>> LibraryCache::get(const std::shared_ptr<const std::string> &source,
const std::map<std::string, std::string> ¯os,
bool disableFastMath,
bool usesInvariance)
{
ASSERT(source != nullptr);
LibraryCache::LibraryCacheEntry &entry =
getCacheEntry(LibraryKey(source, macros, disableFastMath, usesInvariance));
// Try to lock the entry and return the library if it exists. If we can't lock then it means
// another thread is currently compiling.
std::unique_lock<std::mutex> entryLockGuard(entry.lock, std::try_to_lock);
if (entryLockGuard)
{
return entry.library;
}
else
{
return nil;
}
}
angle::ObjCPtr<id<MTLLibrary>> LibraryCache::getOrCompileShaderLibrary(
DisplayMtl *displayMtl,
const std::shared_ptr<const std::string> &source,
const std::map<std::string, std::string> ¯os,
bool disableFastMath,
bool usesInvariance,
angle::ObjCPtr<NSError> *errorOut)
{
id<MTLDevice> metalDevice = displayMtl->getMetalDevice();
const angle::FeaturesMtl &features = displayMtl->getFeatures();
if (!features.enableInMemoryMtlLibraryCache.enabled)
{
return CreateShaderLibrary(metalDevice, *source, macros, disableFastMath, usesInvariance,
errorOut);
}
ASSERT(source != nullptr);
LibraryCache::LibraryCacheEntry &entry =
getCacheEntry(LibraryKey(source, macros, disableFastMath, usesInvariance));
// Lock this cache entry while compiling the shader. This causes other threads calling this
// function to wait and not duplicate the compilation.
std::lock_guard<std::mutex> entryLockGuard(entry.lock);
if (entry.library)
{
return entry.library;
}
entry.library = CreateShaderLibrary(metalDevice, *source, macros, disableFastMath,
usesInvariance, errorOut);
return entry.library;
}
LibraryCache::LibraryCacheEntry &LibraryCache::getCacheEntry(LibraryKey &&key)
{
// Lock while searching or adding new items to the cache.
std::lock_guard<std::mutex> cacheLockGuard(mCacheLock);
auto iter = mCache.Get(key);
if (iter != mCache.end())
{
return iter->second;
}
angle::TrimCache(kMaxCachedLibraries, kGCLimit, "metal library", &mCache);
iter = mCache.Put(std::move(key), LibraryCacheEntry());
return iter->second;
}
LibraryCache::LibraryKey::LibraryKey(const std::shared_ptr<const std::string> &sourceIn,
const std::map<std::string, std::string> ¯osIn,
bool disableFastMathIn,
bool usesInvarianceIn)
: source(sourceIn),
macros(macrosIn),
disableFastMath(disableFastMathIn),
usesInvariance(usesInvarianceIn)
{}
bool LibraryCache::LibraryKey::operator==(const LibraryKey &other) const
{
return std::tie(*source, macros, disableFastMath, usesInvariance) ==
std::tie(*other.source, other.macros, other.disableFastMath, other.usesInvariance);
}
size_t LibraryCache::LibraryKeyHasher::operator()(const LibraryKey &k) const
{
size_t hash = 0;
angle::HashCombine(hash, *k.source);
for (const auto ¯o : k.macros)
{
angle::HashCombine(hash, macro.first);
angle::HashCombine(hash, macro.second);
}
angle::HashCombine(hash, k.disableFastMath);
angle::HashCombine(hash, k.usesInvariance);
return hash;
}
LibraryCache::LibraryCacheEntry::~LibraryCacheEntry()
{
// Lock the cache entry before deletion to ensure there is no other thread compiling and
// preparing to write to the library. LibraryCacheEntry objects can only be deleted while the
// mCacheLock is held so only one thread modifies mCache at a time.
std::lock_guard<std::mutex> entryLockGuard(lock);
}
LibraryCache::LibraryCacheEntry::LibraryCacheEntry(LibraryCacheEntry &&moveFrom)
{
// Lock the cache entry being moved from to make sure the library can be safely accessed.
// Mutexes cannot be moved so a new one will be created in this entry
std::lock_guard<std::mutex> entryLockGuard(moveFrom.lock);
library = std::move(moveFrom.library);
moveFrom.library = nullptr;
}
} // namespace mtl
} // namespace rx