Skip to content
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

Try-catch correctly with dawn-opt #862

Merged
merged 3 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ _local_/*

# Ignore python editable installs
*.egg-info/

# Python venvs
.venv/
21 changes: 6 additions & 15 deletions dawn/src/dawn/Serialization/IIRSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <google/protobuf/util/json_util.h>
#include <memory>
#include <optional>
#include <stdexcept>

namespace dawn {

Expand Down Expand Up @@ -74,8 +75,6 @@ static void setCache(proto::iir::Cache* protoCache, const iir::Cache& cache) {
case iir::Cache::IOPolicy::unknown:
protoCache->set_policy(proto::iir::Cache_CachePolicy_CP_Unknown);
break;
default:
dawn_unreachable("unknown cache policy");
}
switch(cache.getType()) {
case iir::Cache::CacheType::bypass:
Expand All @@ -90,8 +89,6 @@ static void setCache(proto::iir::Cache* protoCache, const iir::Cache& cache) {
case iir::Cache::CacheType::K:
protoCache->set_type(proto::iir::Cache_CacheType_CT_K);
break;
default:
dawn_unreachable("unknown cache type");
}
if(cache.getInterval()) {
auto sirInterval = cache.getInterval()->asSIRInterval();
Expand Down Expand Up @@ -128,7 +125,7 @@ static iir::Cache makeCache(const proto::iir::Cache* protoCache) {
cacheType = iir::Cache::CacheType::K;
break;
default:
dawn_unreachable("unknow cache type");
throw std::out_of_range("unknown cache type");
}
switch(protoCache->policy()) {
case proto::iir::Cache_CachePolicy_CP_BPFill:
Expand All @@ -153,7 +150,7 @@ static iir::Cache makeCache(const proto::iir::Cache* protoCache) {
cachePolicy = iir::Cache::IOPolicy::unknown;
break;
default:
dawn_unreachable("unknown cache policy");
throw std::out_of_range("unknown cache policy");
}
if(protoCache->has_interval()) {
interval = std::make_optional(*makeInterval(protoCache->interval()));
Expand Down Expand Up @@ -283,8 +280,6 @@ void IIRSerializer::serializeIIR(proto::iir::StencilInstantiation& target,
case ast::GridType::Unstructured:
protoIIR->set_gridtype(proto::enums::GridType::Unstructured);
break;
default:
dawn_unreachable("invalid grid type");
}

auto& protoGlobalVariableMap = *protoIIR->mutable_globalvariabletovalue();
Expand Down Expand Up @@ -315,7 +310,7 @@ void IIRSerializer::serializeIIR(proto::iir::StencilInstantiation& target,
protoGlobalToStore.set_type(proto::iir::GlobalValueAndType_TypeKind_Double);
break;
default:
dawn_unreachable("non-supported global type");
throw std::invalid_argument("unsupported global type");
}

protoGlobalToStore.set_valueisset(valueIsSet);
Expand Down Expand Up @@ -495,8 +490,6 @@ IIRSerializer::serializeImpl(const std::shared_ptr<iir::StencilInstantiation>& i
throw std::runtime_error(dawn::format("cannot serialize IIR:"));
break;
}
default:
dawn_unreachable("invalid SerializationKind");
}

return str;
Expand Down Expand Up @@ -644,7 +637,7 @@ void IIRSerializer::deserializeIIR(std::shared_ptr<iir::StencilInstantiation>& t
}
break;
default:
dawn_unreachable("unsupported type");
throw std::out_of_range("unsupported type");
}

target->getIIR()->insertGlobalVariable(std::string(GlobalToValue.first),
Expand Down Expand Up @@ -785,8 +778,6 @@ IIRSerializer::deserializeImpl(const std::string& str, IIRSerializer::Format kin
throw std::runtime_error("cannot deserialize StencilInstantiation");
break;
}
default:
dawn_unreachable("invalid SerializationKind");
}

std::shared_ptr<iir::StencilInstantiation> target;
Expand All @@ -799,7 +790,7 @@ IIRSerializer::deserializeImpl(const std::string& str, IIRSerializer::Format kin
target = std::make_shared<iir::StencilInstantiation>(ast::GridType::Unstructured);
break;
default:
dawn_unreachable("unknown grid type");
throw std::out_of_range("unknown grid type");
}

int maxID = 0;
Expand Down
28 changes: 13 additions & 15 deletions dawn/src/dawn/Serialization/SIRSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <list>
#include <memory>
#include <stack>
#include <stdexcept>
#include <tuple>

namespace dawn {
Expand Down Expand Up @@ -116,8 +117,6 @@ static std::string serializeImpl(const SIR* sir, SIRSerializer::Format kind) {
case ast::GridType::Unstructured:
sirProto.set_gridtype(proto::enums::GridType::Unstructured);
break;
default:
dawn_unreachable("invalid grid type");
}

// SIR.Filename
Expand Down Expand Up @@ -163,7 +162,7 @@ static std::string serializeImpl(const SIR* sir, SIRSerializer::Format kind) {
} else if(sir::Offset* offset = dyn_cast<sir::Offset>(arg.get())) {
setOffset(argProto->mutable_offset_value(), offset);
} else {
dawn_unreachable("invalid argument");
throw std::invalid_argument("Invalid argument: StencilFunction.Args");
}
}

Expand Down Expand Up @@ -230,8 +229,6 @@ static std::string serializeImpl(const SIR* sir, SIRSerializer::Format kind) {
"cannot deserialize SIR: %s", ProtobufLogger::getInstance().getErrorMessagesAndReset()));
break;
}
default:
dawn_unreachable("invalid SerializationKind");
}

return str;
Expand Down Expand Up @@ -471,7 +468,7 @@ static std::shared_ptr<sir::Expr> makeExpr(const dawn::proto::statements::Expr&
offset = ast::Offsets{ast::HorizontalOffset{}, exprProto.vertical_offset()};
break;
default:
dawn_unreachable("unknown offset");
throw std::invalid_argument("Unknown offset");
}

Array3i argumentOffset{{0, 0, 0}};
Expand Down Expand Up @@ -519,11 +516,12 @@ static std::shared_ptr<sir::Expr> makeExpr(const dawn::proto::statements::Expr&
weights.push_back(dawn::sir::Value(weightProto.integer_value()));
break;
case proto::statements::Weight::kStringValue:
dawn_unreachable("string type for weight encountered in serialization (weights need to be "
"of arithmetic type)\n");
throw std::invalid_argument(
"string type for weight encountered in serialization (weights need to be "
"of arithmetic type)\n");
break;
case proto::statements::Weight::VALUE_NOT_SET:
dawn_unreachable("weight with undefined value encountered!\n");
throw std::invalid_argument("weight with undefined value encountered!\n");
break;
}
}
Expand All @@ -542,7 +540,7 @@ static std::shared_ptr<sir::Expr> makeExpr(const dawn::proto::statements::Expr&
}
case dawn::proto::statements::Expr::EXPR_NOT_SET:
default:
dawn_unreachable("expr not set");
throw std::out_of_range("expr not set");
}
return nullptr;
}
Expand Down Expand Up @@ -610,7 +608,7 @@ static std::shared_ptr<sir::Stmt> makeStmt(const dawn::proto::statements::Stmt&
}
case dawn::proto::statements::Stmt::STMT_NOT_SET:
default:
dawn_unreachable("stmt not set");
throw std::out_of_range("stmt not set");
}
return nullptr;
}
Expand Down Expand Up @@ -644,7 +642,7 @@ static std::shared_ptr<SIR> deserializeImpl(const std::string& str, SIRSerialize
break;
}
default:
dawn_unreachable("invalid serialization Kind");
throw std::invalid_argument("invalid serialization Kind");
}

// Convert protobuf SIR to SIR
Expand All @@ -661,7 +659,7 @@ static std::shared_ptr<SIR> deserializeImpl(const std::string& str, SIRSerialize
sir = std::make_shared<SIR>(ast::GridType::Unstructured);
break;
default:
dawn_unreachable("unknown grid type");
throw std::out_of_range("Unknown grid type");
}

// SIR.Filename
Expand Down Expand Up @@ -712,7 +710,7 @@ static std::shared_ptr<SIR> deserializeImpl(const std::string& str, SIRSerialize
break;
case dawn::proto::statements::StencilFunctionArg::ARG_NOT_SET:
default:
dawn_unreachable("argument not set");
throw std::out_of_range("argument not set");
}
}

Expand Down Expand Up @@ -753,7 +751,7 @@ static std::shared_ptr<SIR> deserializeImpl(const std::string& str, SIRSerialize
break;
case sir::proto::GlobalVariableValue::VALUE_NOT_SET:
default:
dawn_unreachable("value not set");
throw std::out_of_range("value not set");
}

sir->GlobalVariableMap->emplace(sirName, std::move(*value));
Expand Down
10 changes: 6 additions & 4 deletions dawn/src/dawn/dawn-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ deserializeInput(const std::string& input) {
dawn::SIRSerializer::deserializeFromString(input, dawn::SIRSerializer::Format::Byte);
format = SerializationFormat::Byte;
} catch(...) {
// Do nothing
stencilIR = nullptr;
}
}
if(!stencilIR) {
Expand All @@ -100,7 +100,7 @@ deserializeInput(const std::string& input) {
dawn::SIRSerializer::deserializeFromString(input, dawn::SIRSerializer::Format::Json);
format = SerializationFormat::Json;
} catch(...) {
// Do nothing
stencilIR = nullptr;
}
}
// Then try IIR
Expand All @@ -111,7 +111,7 @@ deserializeInput(const std::string& input) {
dawn::IIRSerializer::deserializeFromString(input, dawn::IIRSerializer::Format::Byte);
format = SerializationFormat::Byte;
} catch(...) {
// Do nothing
internalIR = nullptr;
}
}
if(!internalIR && !stencilIR) {
Expand All @@ -120,7 +120,7 @@ deserializeInput(const std::string& input) {
dawn::IIRSerializer::deserializeFromString(input, dawn::IIRSerializer::Format::Json);
format = SerializationFormat::Json;
} catch(...) {
// Do nothing
internalIR = nullptr;
}
}

Expand All @@ -134,6 +134,7 @@ deserializeInput(const std::string& input) {
stencilIR =
dawn::SIRSerializer::deserializeFromString(input, dawn::SIRSerializer::Format::Json);
}
break;
}
case IRType::IIR: {
if(format == SerializationFormat::Byte) {
Expand All @@ -143,6 +144,7 @@ deserializeInput(const std::string& input) {
internalIR =
dawn::IIRSerializer::deserializeFromString(input, dawn::IIRSerializer::Format::Json);
}
break;
}
}

Expand Down