Skip to content
This repository has been archived by the owner on Jan 19, 2025. It is now read-only.

Kuznetsov Artyom lab2 (option 1) #54

Conversation

Kuznetsov-Artyom
Copy link

@Kuznetsov-Artyom Kuznetsov-Artyom commented Mar 25, 2024

Implemented a plugin that inserts the instruction_start()/insrtuction_end() functions at the beginning and end of the function, respectively.

Example:

// test.cpp
int factorial(int value) {
  return value <= 1 ? 1 : value * factorial(value - 1);
}
; test.ll
define dso_local noundef i32 @_Z9factoriali(i32 noundef %value) {
entry:
  %value.addr = alloca i32, align 4
  store i32 %value, ptr %value.addr, align 4
  %0 = load i32, ptr %value.addr, align 4
  %cmp = icmp sle i32 %0, 1
  br i1 %cmp, label %cond.true, label %cond.false

cond.true:
  br label %cond.end

cond.false:
  %1 = load i32, ptr %value.addr, align 4
  %2 = load i32, ptr %value.addr, align 4
  %sub = sub nsw i32 %2, 1
  %call = call noundef i32 @_Z9factoriali(i32 noundef %sub)
  %mul = mul nsw i32 %1, %call
  br label %cond.end

cond.end:
  %cond = phi i32 [ 1, %cond.true ], [ %mul, %cond.false ]
  ret i32 %cond
}

Result:

; test_opt.ll
define dso_local noundef i32 @_Z9factoriali(i32 noundef %value) {
entry:
  call void @instrument_start()
  %value.addr = alloca i32, align 4
  store i32 %value, ptr %value.addr, align 4
  %0 = load i32, ptr %value.addr, align 4
  %cmp = icmp sle i32 %0, 1
  br i1 %cmp, label %cond.true, label %cond.false

cond.true:
  br label %cond.end

cond.false:
  %1 = load i32, ptr %value.addr, align 4
  %2 = load i32, ptr %value.addr, align 4
  %sub = sub nsw i32 %2, 1
  %call = call noundef i32 @_Z9factoriali(i32 noundef %sub)
  %mul = mul nsw i32 %1, %call
  br label %cond.end

cond.end:
  %cond = phi i32 [ 1, %cond.true ], [ %mul, %cond.false ]
  call void @instrument_end()
  ret i32 %cond
}

@Kuznetsov-Artyom
Copy link
Author

Windows crashes for some reason, I'll figure it out tomorrow.

@Kuznetsov-Artyom Kuznetsov-Artyom changed the title Kuznetsov artyom lab2 (option 1) Kuznetsov Artyom lab2 (option 1) Mar 26, 2024
@Kuznetsov-Artyom Kuznetsov-Artyom requested a review from m-ly4 March 29, 2024 11:13
Copy link

@m-ly4 m-ly4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for patient applying remarks)
Very good.

@m-ly4 m-ly4 merged commit f5dec44 into NN-complr-tech:course-spring-2024 Mar 29, 2024
5 checks passed
@Kuznetsov-Artyom Kuznetsov-Artyom deleted the kuznetsov_artyom_lab2 branch March 29, 2024 19:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants