-
Notifications
You must be signed in to change notification settings - Fork 17
Centralize target description query through DLTI and add verifier pass #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//===-- TargetDescriptionAnalysis.h - target description class --*- C++ -*-===// | ||
// | ||
// This file is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_ANALYSIS_TARGETDESCRIPTIONANALYSIS_H | ||
#define MLIR_ANALYSIS_TARGETDESCRIPTIONANALYSIS_H | ||
|
||
#include "gc/Dialect/Linalgx/LinalgxOps.h" | ||
#include "mlir/Dialect/DLTI/DLTI.h" | ||
#include "mlir/Dialect/Linalg/IR/Linalg.h" | ||
#include "mlir/Interfaces/DataLayoutInterfaces.h" | ||
#include "llvm/ADT/StringRef.h" | ||
|
||
namespace mlir { | ||
namespace gc { | ||
|
||
using namespace mlir; | ||
|
||
enum DeviceType { CPU = 0 }; | ||
|
||
class TargetDescriptionAnalysisBase { | ||
public: | ||
TargetDescriptionAnalysisBase(Operation *op, DeviceType device) | ||
: ctx(op->getContext()), device(device), | ||
layout(isa<ModuleOp>(op) ? dyn_cast<ModuleOp>(op) | ||
: op->getParentOfType<ModuleOp>()), | ||
loc(op->getLoc()) {} | ||
|
||
// get the device ID | ||
DeviceType getDevice() { return device; } | ||
|
||
// get the MLIR context | ||
MLIRContext *getContext() { return ctx; } | ||
|
||
// get the data layout | ||
DataLayout getLayout() { return layout; } | ||
|
||
// get the property value by key | ||
std::optional<Attribute> getPropertyValue(StringRef key); | ||
|
||
// get the location | ||
Location getLocation() { return loc; } | ||
|
||
// check if the property exists | ||
bool hasProperty(StringRef key) { return getPropertyValue(key).has_value(); } | ||
|
||
// emit warning if the property is not found | ||
template <typename T> | ||
void emitNotFoundWarning(Location loc, StringRef key, T value); | ||
|
||
// the map from device type to device string | ||
static llvm::DenseMap<DeviceType, std::string> DeviceKeyMap; | ||
|
||
private: | ||
MLIRContext *ctx; | ||
DeviceType device; | ||
DataLayout layout; | ||
Location loc; | ||
}; | ||
|
||
class CPUTargetDescriptionAnalysis : public TargetDescriptionAnalysisBase { | ||
public: | ||
static constexpr StringLiteral kL1CacheSize = "L1_cache_size_in_bytes"; | ||
static constexpr StringLiteral kL2CacheSize = "L2_cache_size_in_bytes"; | ||
static constexpr StringLiteral kL3CacheSize = "L3_cache_size_in_bytes"; | ||
static constexpr StringLiteral kMaxVectorWidth = "max_vector_width"; | ||
static constexpr StringLiteral kNumThreads = "num_threads"; | ||
|
||
// get runtime OMP_NUM_THREADS | ||
size_t getNumThreads(); | ||
|
||
// get cache size by cacheLevel | ||
size_t getCacheSize(uint8_t cacheLevel); | ||
|
||
// get the maximum vector length in bits | ||
size_t getMaxVectorWidth(); | ||
|
||
// get the default value map(attr key, default value) | ||
static llvm::DenseMap<StringRef, int64_t> CPUTargetDeafultValueMap; | ||
|
||
CPUTargetDescriptionAnalysis(Operation *op) | ||
: TargetDescriptionAnalysisBase(op, DeviceType::CPU) {} | ||
}; | ||
|
||
} // namespace gc | ||
} // namespace mlir | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
gc_set_mlir_link_components(MLIR_LINK_COMPONENTS | ||
MLIRIR | ||
MLIRSupport) | ||
|
||
gc_add_mlir_library(GcAnalysis | ||
TargetDescriptionAnalysis.cpp | ||
|
||
DEPENDS | ||
GraphCompilerPassIncGen | ||
|
||
LINK_LIBS PUBLIC | ||
${mlir_dialect_libs} | ||
${MLIR_LINK_COMPONENTS} | ||
GcInterface | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//===-- TargetDescriptionAnalysis.cpp - target description impl -*- C++ -*-===// | ||
// | ||
// This file is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "gc/Analysis/TargetDescriptionAnalysis.h" | ||
#include <limits> | ||
#include <llvm/Support/Debug.h> | ||
#include <regex> | ||
|
||
namespace mlir { | ||
namespace gc { | ||
|
||
#define DEBUG_TYPE "target-description-analysis" | ||
|
||
llvm::DenseMap<DeviceType, std::string> | ||
TargetDescriptionAnalysisBase::DeviceKeyMap = { | ||
{CPU, "CPU"}, | ||
}; | ||
|
||
// default values for properties | ||
llvm::DenseMap<StringRef, int64_t> | ||
CPUTargetDescriptionAnalysis::CPUTargetDeafultValueMap = { | ||
{CPUTargetDescriptionAnalysis::kNumThreads, 1}, | ||
{CPUTargetDescriptionAnalysis::kL1CacheSize, 32 * 1024}, | ||
{CPUTargetDescriptionAnalysis::kL2CacheSize, 1024 * 1024}, | ||
{CPUTargetDescriptionAnalysis::kL3CacheSize, 32 * 1024 * 1024}, | ||
{CPUTargetDescriptionAnalysis::kMaxVectorWidth, 512}, | ||
}; | ||
|
||
template <typename T> | ||
void TargetDescriptionAnalysisBase::emitNotFoundWarning(Location loc, | ||
StringRef key, | ||
T value) { | ||
mlir::emitWarning(loc) << key << " not found, using default value " << value; | ||
} | ||
|
||
static bool isIntegerNumber(const std::string &token) { | ||
return std::regex_match(token, std::regex(("(\\+|-)?[[:digit:]]+"))); | ||
} | ||
|
||
static int64_t getIntFromAttribute(Attribute attr) { | ||
if (auto intAttr = dyn_cast<IntegerAttr>(attr)) { | ||
if (intAttr.getType().isSignedInteger()) | ||
return intAttr.getSInt(); | ||
else if (intAttr.getType().isUnsignedInteger()) | ||
return intAttr.getUInt(); | ||
else | ||
return intAttr.getInt(); | ||
} else if (auto strAttr = dyn_cast<StringAttr>(attr)) { | ||
std::string str = strAttr.getValue().str(); | ||
if (isIntegerNumber(str)) | ||
return std::stoll(str); | ||
} | ||
llvm_unreachable("Not an integer attribute or integer like string attribute"); | ||
} | ||
|
||
std::optional<Attribute> | ||
TargetDescriptionAnalysisBase::getPropertyValue(StringRef key) { | ||
return layout.getDevicePropertyValue( | ||
Builder(getContext()) | ||
.getStringAttr(DeviceKeyMap[getDevice()] /* device ID*/), | ||
Builder(getContext()).getStringAttr(key)); | ||
} | ||
|
||
size_t CPUTargetDescriptionAnalysis::getNumThreads() { | ||
std::optional<Attribute> numThreads = getPropertyValue(kNumThreads); | ||
|
||
if (numThreads) | ||
return getIntFromAttribute(*numThreads); | ||
emitNotFoundWarning(getLocation(), kNumThreads, | ||
CPUTargetDeafultValueMap[kNumThreads]); | ||
return CPUTargetDeafultValueMap[kNumThreads]; | ||
} | ||
|
||
size_t CPUTargetDescriptionAnalysis::getCacheSize(uint8_t cacheLevel) { | ||
assert(cacheLevel > 0 && cacheLevel < 4 && "Invalid cache level"); | ||
StringLiteral key = ""; | ||
if (cacheLevel == 1) | ||
key = kL1CacheSize; | ||
else if (cacheLevel == 2) | ||
key = kL2CacheSize; | ||
else if (cacheLevel == 3) | ||
key = kL3CacheSize; | ||
|
||
std::optional<Attribute> cacheSize = getPropertyValue(key); | ||
if (cacheSize) | ||
return getIntFromAttribute(*cacheSize); | ||
|
||
emitNotFoundWarning(getLocation(), key, CPUTargetDeafultValueMap[key]); | ||
return CPUTargetDeafultValueMap[key]; | ||
} | ||
|
||
size_t CPUTargetDescriptionAnalysis::getMaxVectorWidth() { | ||
std::optional<Attribute> maxVectorWidth = getPropertyValue(kMaxVectorWidth); | ||
if (maxVectorWidth) | ||
return getIntFromAttribute(*maxVectorWidth); | ||
emitNotFoundWarning(getLocation(), kMaxVectorWidth, | ||
CPUTargetDeafultValueMap[kMaxVectorWidth]); | ||
return CPUTargetDeafultValueMap[kMaxVectorWidth]; | ||
} | ||
|
||
} // namespace gc | ||
} // namespace mlir |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
set(GC_ALL_LIBS | ||
${GC_ONEDNN_DIALECT_LIB_NAME} | ||
GcPasses | ||
GcAnalysis | ||
MLIRCPURuntimeTransforms) | ||
|
||
if(GC_ENABLE_IMEX) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
add_subdirectory(Analysis) | ||
add_subdirectory(CAPI) | ||
add_subdirectory(Dialect) | ||
add_subdirectory(Transforms) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,5 @@ gc_add_mlir_library(GcJitWrapper | |
${dialect_libs} | ||
${conversion_libs} | ||
${GC_PASSES} | ||
GcAnalysis | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.