Skip to content

Commit

Permalink
verification + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitoleg committed May 8, 2024
1 parent c40d1b3 commit 86a4555
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/Support/ErrorHandling.h"
#include <numeric>
#include <optional>
#include <set>

#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
Expand Down Expand Up @@ -2174,6 +2175,25 @@ LogicalResult cir::FuncOp::verify() {
<< "' must have empty body";
}

std::set<llvm::StringRef> labels;
std::set<llvm::StringRef> gotos;

getOperation()->walk([&](mlir::Operation *op) {
if (auto lab = dyn_cast<mlir::cir::LabelOp>(op)) {
labels.emplace(lab.getLabel());
} else if (auto goTo = dyn_cast<mlir::cir::GotoOp>(op)) {
gotos.emplace(goTo.getLabel());
}
});

std::vector<llvm::StringRef> mismatched;
std::set_difference(gotos.begin(), gotos.end(),
labels.begin(), labels.end(),
std::back_inserter(mismatched));

if (!mismatched.empty())
return emitOpError() << "goto/label mismatch";

return success();
}

Expand Down
11 changes: 11 additions & 0 deletions clang/test/CIR/CodeGen/goto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ void severalGotosInARow(int a) {
// NOFLAT: ^bb[[#BLK2:]]:
// NOFLAT: cir.label "end"


void labelWithoutMatch() {
end:
return;
}
// NOFLAT: cir.func @_Z17labelWithoutMatchv()
// NOFLAT: cir.label "end"
// NOFLAT: cir.return
// NOFLAT: }


int jumpIntoLoop(int* ar) {

if (ar)
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CIR/IR/invalid.cir
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,14 @@ module {
%0 = cir.dyn_cast(ptr, %arg0 : !cir.ptr<!Base>, #cir.dyn_cast_info<#cir.global_view<@_ZTI4Base> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI7Derived> : !cir.ptr<!u32i>, @__dynamic_cast, @__cxa_bad_cast, #cir.int<0> : !s64i>) -> !cir.ptr<!Derived>
}
}


// -----

// expected-error@+1 {{goto/label mismatch}}
cir.func @bad_goto() -> () {
cir.goto "somewhere"
^bb1:
cir.label "label"
cir.return
}

0 comments on commit 86a4555

Please sign in to comment.