diffkemp icon indicating copy to clipboard operation
diffkemp copied to clipboard

Tests in llvm ir

Open PLukas2018 opened this issue 10 months ago • 4 comments

Introduction to unit testing of DifferentialFunctionComparator using LLVM IR. Tests can be written like this:

TEST_F(DFCLlvmIrTest, ReorderedBinaryOperationDifferentOperands) {
    auto left = R"(define i8 @f() {
        %1 = add i8 0, 0
        ret i8 %1
    })";
    auto right = R"(define i8 @f() {
        %1 = add i8 1, 0
        ret i8 %1
    })";
    CREATE_FROM_LLVM(left, right);
    ASSERT_EQ(DiffComp->compare(), 1);
}

PR contains 12 rewritten unit tests from 13 which were using only compare method, (the last one is working with a custom pattern, which should also be rewritable but needs additional logic).

There are more than 28 other tests which test a specific DifferentialFunctionComparator method, some of these tests could be also rewritable: a) by rewriting it and testing it as whole - we will lose the level of detail, b) by testing specific instructions for eg. like this:

/// Tests a comparison of two GEPs of a structure type with indices compared by
/// value.
TEST_F(DFCLlvmIrTest, CmpGepsSimple) {
    auto left = R"(
        %struct = type { i8, i16 }
        define void @f() {
            %var = alloca %struct
            %gep1 = getelementptr %struct, %struct* %var, i32 0, i32 0
            %gep2 = getelementptr %struct, %struct* %var, i32 0, i32 0
            ret void
        }
    )";

    auto right = R"(
        %struct = type { i8, i16 }
        define void @f() {
            %var = alloca %struct
            %gep1 = getelementptr %struct, %struct* %var, i32 0, i32 0
            %gep2 = getelementptr %struct, %struct* %var, i32 0, i32 1
            ret void
        }
    )";
    CREATE_FROM_LLVM(left, right);
    ASSERT_EQ(testCmpGEPs("gep1"), 0);
    ASSERT_EQ(testCmpGEPs("gep2"), 1);
}

PLukas2018 avatar Apr 04 '24 15:04 PLukas2018

Btw for now I used typed pointer in LLVM IR, therefore the tests are run on all LLVM versions which we test in CI. I tried to parse typed pointer in LLVM 18 and it worked, so probably the not supporting typed pointer is meant only in API

Typed pointers are no longer supported and the -opaque-pointers option has been removed. See the opaque pointers documentation for migration instructions.

https://releases.llvm.org/17.0.1/docs/ReleaseNotes.html#id5

PLukas2018 avatar Apr 04 '24 16:04 PLukas2018

b) by testing specific instructions for eg. like this:

    [...]
    CREATE_FROM_LLVM(left, right);
    ASSERT_EQ(testCmpGEPs("gep1"), 0);
    ASSERT_EQ(testCmpGEPs("gep2"), 1);
}

This looks great, that would be a nice functionality! But let's leave it to a follow-up PR.

I'll wait with reviewing this one until you move it out of the Draft state.

viktormalik avatar Apr 05 '24 06:04 viktormalik

Btw for now I used typed pointer in LLVM IR, therefore the tests are run on all LLVM versions which we test in CI. I tried to parse typed pointer in LLVM 18 and it worked, so probably the not supporting typed pointer is meant only in API

I'm a bit surprised that LLVM 18 can parse IR files with explicit pointers but if that works, let's use it for now. If some future LLVM version stops supporting this, it should be easy to rewrite it to opaque pointers.

viktormalik avatar Apr 05 '24 06:04 viktormalik

I'll wait with reviewing this one until you move it out of the Draft state.

I moved it out of draft, but the LLVM IR tests will maybe make more sense to move to a new file, but for now I am leaving it as it is so it easier to compare the rewritten tests.

PLukas2018 avatar Apr 05 '24 08:04 PLukas2018

Thanks, I cleaned up the commits and rebased it on master.

PLukas2018 avatar May 18 '24 09:05 PLukas2018

b) by testing specific instructions for eg. like this:

    [...]
    CREATE_FROM_LLVM(left, right);
    ASSERT_EQ(testCmpGEPs("gep1"), 0);
    ASSERT_EQ(testCmpGEPs("gep2"), 1);
}

This looks great, that would be a nice functionality! But let's leave it to a follow-up PR.

Implemented in #358 PR.

PLukas2018 avatar Jul 11 '24 13:07 PLukas2018