This is one jazzed-up look at the life and times of the trumpet god Miles Davis.

We’re in the 1970s, and then we’re in the 1980s, and then it’s back to the 1950s, and now we’re in the middle of a slapstick crime caper, and NOW we’re at a boxing match, but all of a sudden Davis is playing his trumpet in the middle of the ring while another version of Davis is involved in a fracas in the stands, and WAIT WHAT IS HAPPENING?

Cheadle the director, producer and co-writer boldly goes for broke with mixed results in this highly fictionalized version of the Miles Davis legend — and Cheadle the actor gives a brilliant performance worthy of an Oscar nomination.

Sporting a period-perfect ‘fro, colorful clothes, oversized sunglasses and serious jewelry, sweating like a man with a serious drug problem and speaking in a mesmerizing, raspy tone, Cheadle — who looks nothing like Miles Davis in real life — absolutely inhabits the character, to the point where even the fine actors sharing the screen with him almost blur into the background.

Davis was a deeply flawed, mercurial figure who indulged in just about every bad habit available at the time, but also produced some of the most electric music of the century. Cheadle is alternately provocative, wickedly funny, repulsive, magnetic and mesmerizing as Davis.

“Miles Ahead” begins in the late 1970s, at a time when Davis hadn’t picked up the trumpet in years and was living in virtual seclusion. One day a Composite Character Plot Device, aka Ewan McGregor’s slimy hustler of a freelance journalist named Dave, shows up at Davis’ door, trying to finagle an interview.

RELATED: Don Cheadle was ‘so stressed out’ directing and starring as Miles Davis

Dave somehow becomes Miles’ hapless sidekick, accompanying him on some dubious missions and often getting beaten up. It’s a goofy role and McGregor gives it a goofy spin, treading the line between irritating and amusing.

A main plot thread of “Miles Ahead” concerns the pursuit of some master session tapes said to contain perhaps the best work ever done by Davis.

Michael Stuhlbarg excels as a shady lawyer who purloins the tapes. Miles and the journalist Dave team up to retrieve the tapes — but we’re not so sure Dave won’t steal the tapes himself if he gets the chance. The battle for control of the tapes leads to violent confrontations and car chases that seem lifted from an entirely different movie.

“Miles Ahead” is much more interesting when Davis talks about his art (“it’s not jazz, it’s social music”) and looks back on his tumultuous romance with his first wife and the love of his life, Frances Taylor (Emayatzy Corinealdi in an outstanding supporting performance). Cheadle the director doesn’t shy away from Davis’ ugly side, including his violent outbursts, his womanizing, his heavy drug habit and his self-destructive tendencies.

(In other words, Davis as depicted in this film isn’t much different from the subjects of a dozen other biopics about 20th century musical legends, from Johnny Cash to Jerry Lee Lewis to Ray Charles to Charlie Parker to Hank Williams. Rarely have we seen a film about a musical genius without the word “troubled” prefacing the genius part.)

So much of this film is pure fiction. The journalist is not based on any real-life scribe. A trumpet prodigy (Lakeith Lee Stanfield) who shares Davis’ raw talent as well as his drug habit is also a product of the film’s imagination. Some of the more stylized scenes come across as overly ambitious attempts to create the cinematic equivalent of a Miles Davis record — but I give credit to Cheadle for trying to do something to separate “Miles Ahead” from other music biopics. Even when the story goes off the rails, it’s almost never uninteresting.

“Miles Ahead” of course crackles with some of Davis’ best works, from the title track to “So What” to “Solea” to “Black Satin.” Cheadle learned to play trumpet so he could more effectively mimic Davis’ style, and he looks every inch the jazz legend in the performance and recording scenes.

In a rousing, beautifully shot closing sequence, Davis shares the stage with modern-day jazz musicians. Like most of the film, it’s a bit loopy and a bit trippy, but pretty damn cool as well.

★★★

Sony Pictures Classics presents a film directed by Don Cheadle and written by Cheadle and Steven Baigelman. Running time: 100 minutes. Rated R (for strong language throughout, drug use, some sexuality/nudity and brief violence.). Opens Friday at local theaters.